Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a sharepoint hosted app that is succesfully able to add Read and Create files on the Host site (using the REST Api and SP.RequestExecutor). Now I am trying to figure out how to update properties. Specifically the SP.View.jsLink property.

According to the documentation I should be able to POST to the following url to set it? What is the request body supposed to look like? Do I have to then post to the update method on the view to save it?

http://<sitecollection>/<site>/_api/web/lists(listid)/Views(viewid)/jsLink

Posting to the above url (experimenting w/ different request body values), comes back 200, but doesn't update the property. Then I tried performing a PATCH on the entire SP.View object, according to Microsoft's example. The PATCH (and MERGE and PUT) throws the following error.

{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"An entry without a type name was found, but no expected type was specified. To allow entries without type information, the expected type must also be specified when the model is specified."}}}
share|improve this question
add comment

2 Answers

I got this working using JSOM. I'm not happy about it though. It feels wierd to mix REST with JSOM.

//Initilize JSOM contexts inside constructor
var DataService = function(appUrl, hostUrl) {
    var self = this;
    //Some other stuff not related to this...

    self.appContext = new SP.ClientContext(appUrl);
    var factory = new SP.ProxyWebRequestExecutorFactory(appUrl);
    self.appContext.set_webRequestExecutorFactory(factory);
    self.hostContext = new SP.AppContextSite(self.appContext, hostUrl);
};

//Update jsLink
DataService.prototype.setJsLink = function(listTitle, viewTitle, templateName) {
    var self = this,
        deferred = new $.Deferred(),
        web = self.hostContext.get_web(),
        view = web.get_lists().getByTitle(listTitle).get_views().getByTitle(viewTitle);

    view.set_jsLink("~site/SiteAssets/" + templateName);
    view.update();
    self.appContext.executeQueryAsync(deferred.resolve, deferred.reject);

    return deferred;
};

Don't forget to give your app permissions in the AppManifest.xml

share|improve this answer
add comment

The solution I found was to simply include the __metadata property as part of the PATCH request. So, instead of just sending the POST data:

{
    Id: 1,
    Title: "Something"
}

we had to send:

{
    Id: 1,
    Title: "Something",
    __metadata: {
        type: "SP.Data.NameOfOurObject"
    }
}

We also included the etag, id and uri portions of the metadata, but I do not believe those are required for the request to be successful.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.