从 Apache Web 服务器通过 Javascript 创建 SharePoint 2013 列表项



我有一个网站托管在Apache网络服务器上,带有自定义html表单。我还有一个 SharePoint 2013 列表(在同一网络上(,我需要自定义 html 表单来向其添加项目。到目前为止,我一直在研究 SP 的 JSOM(特别是 sp.js 等(。

我还考虑了在 SharePoint 本身上托管表单,取得了更大的成功,因为它看起来像 sp.js 期待一个相对 url,这对于 Apache 服务器没有意义。

到目前为止的JS代码

function save(){
if(confirm("Are you sure you want to submit this?")){
ExecuteOrDelayUntilScriptLoaded(createListItem, "sp.js");
}
}


function createListItem() {
var siteUrl= "/sites/mysites/site";
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Sandbox');
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Title', 'My New Item!');
oListItem.set_item('Line Type', 'Type 1');
oListItem.set_item('Amount', '100');
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded), 
Function.createDelegate(this, this.onQueryFailed)
);
}
function onQuerySucceeded() {
alert('Item created: ' + oListItem.get_id());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + 
'n' + args.get_stackTrace());
}

网页页眉

<head>
<script src="http://mysite/sites/site/_layouts/1033/init.js" type="text/javascript"></script>
<script src="http://mysite/sites/site/_layouts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="http://mysite/sites/site/_layouts/sp.core.js" type="text/javascript"></script>
<script src="http://mysite/sites/site/_layouts/sp.runtime.js" type="text/javascript"></script>
<script src="http://mysite/sites/site/_layouts/sp.js" type="text/javascript"></script>
<script src="TER.js"></script>
</head>

请注意,在Javascript中,我根据需要更改变量siteUrl,具体取决于我当前正在测试的Web主机(即绝对或相对(。

有谁知道我正在尝试做的事情是否可行,或者我需要尝试不同的方法?

您可以使用https://aymkdn.github.io/SharepointPlus/或 http://sympmarc.github.io/SPServices/与使用Javascript和AJAX的Sharepoint列表和库进行交互。

与 CORS 相关,直接在 SharePoint 上托管自定义 HTML 表单更容易。我通过在 SharePoint 文档库中托管我的 HTML 表单和 JS 文件来使用此方法。

使用我最喜欢的 SharepointPlus,您可以添加包含以下内容的列表项:

$SP().list("ListName", "http://path/to/sharepoint/site/collection").add({ Title: "foobar" });

亲切问候

最新更新