Вопрос

Is it possible to insert data in a SharePoint 2013 list without using a SP App? I need to insert some data from our CMS into a SharePoint list, and can't figure out how to connect to the SP instance.

I've tried using the supplied TokenHelper, but all my trials results in either 400 Bad Request or 403 Forbidden.

UPDATE: I'd prefer to use C# for this, not the JavaScript library.

Это было полезно?

Решение

You can follow Kirk Evan's blog post, Call O365 Using CSOM with a Console Application. Don't have to bother with all the oAuth/App stuff. Worked rather well for me in a similar situation. Hope this helps out.

Другие советы

If your CMS is written in .NET, you could use the Client Side Object Model. Your code would look something like this:

function _createListItem(listName, values, logProgress) { 
    var context = new SP.ClientContext();
    var dfd = $.Deferred();
    var list = context.get_web().get_lists().getByTitle(listName);
    context.load(list);
    var itmCI = new SP.ListItemCreationInformation();
    this.itm = list.addItem(itmCI);
    for (fieldName in values)
    {
        this.itm.set_item(fieldName, values[fieldName]);
    }
    this.itm.update();
    context.load(this.itm);
    context.executeQueryAsync(
        function () {
            dfd.resolve();
        },
        function (sender, args) {
            dfd.reject(sender, args, "Error creating ListItem");
        });
    return dfd.promise();
}

If your CMS is written in something else, you could use REST. The code would look something like this:

function _createListItem(listName, values) {

    var dfd = $.Deferred();
    var baseUrl = SP.Utilities.UrlBuilder.urlCombine(
        _spPageContextInfo.webServerRelativeUrl,
        "_api/web/lists/");
    baseUrl += "GetByTitle('" + listName + "')/items";
    values["__metadata"] = { "type": "SP.Data." + listName + "ListItem" };
    var itemData = JSON.stringify(values);
    var headers = {
        "accept": "application/json;odata=verbose",
        "content-length": itemData.length,
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    }
    var dfd = $.ajax({
        url: encodeURI(baseUrl),
        type: "POST",
        contentType: "application/json;odata=verbose", 
        data: itemData,
        headers: headers
    });
    return dfd.promise();
}

You'd need to make a call to /ContextInfo to get the X-RequestDigest value since you're not in an SP-App. Also, this code is written in JavaScript, so you'd need to deal with cross-domain issues by using the cross domain library. If you're doing REST in another language, the code would be similar, you can specify credentials for the REST call and you won't have to deal with cross-domain issues.

If you need more info, please specify which language you're using so I can focus on that.

HTH.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top