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

3 Answers 3

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

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

In order to update JSLink property of a View, the following conditions must to be met:

  • proper View resource endpoint url, for example: /_api/web/lists/getbytitle('listTitle')/views/getbytitle('viewTitle')
  • entity type for View resource set to : '__metadata': {'type': 'SP.View'}

How to update List View using SharePoint 2013 REST

function postJson(endpointUri,payload,isUpdate, success, error) 
{
    var headers = {};
    headers["Accept"] = "application/json;odata=verbose";
    headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
    if(isUpdate) {
       headers["X-HTTP-Method"] = "MERGE";
       headers["If-Match"] = "*";
    }   

    $.ajax({       
       url: endpointUri,   
       type: "POST",   
       processData: false,  
       data: JSON.stringify(payload),
       contentType: "application/json;odata=verbose",
       headers: headers, 
       success: success,
       error: error
    });
}



function updateView(webUrl,listTitle,viewTitle,viewProperties, success,failure)
{
    //construct View resource endpoint url
    var viewUri =  webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/views/getbytitle('" + viewTitle + "')";
    //prepare View resource payload
    var viewPayload = {
       '__metadata': {'type': 'SP.View'}
    };
    for(var prop in viewProperties){
         viewPayload[prop] = viewProperties[prop];
    }

    postJson(viewUri,viewPayload,true,
       function(data){
          success(); 
        },
       failure);
}

Gist: updateView.js

Usage

The following example demonstrates how to update JSLink property for a View using SharePoint REST:

var viewProperties = {'JSLink':'hierarchytaskslistex.js'};

updateView(_spPageContextInfo.webAbsoluteUrl,'Tasks','All Tasks',viewProperties,printInfo,logError);
function printInfo(view)
{
    console.log('View has been updated');
}
function logError(error){
    console.log(JSON.stringify(error));
}
share|improve this answer

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.