Today I’d like to share with you how to retrieve an item of a specific type and update it’s list item properties in SharePoint 2013 Apps. Pretty basic task, nevertheless not a lot of obvious results are out there when doing a search.
So in my case I’m assuming you have a document library and you have a file in there and you’d like to update properties of the list item for that file;
In our case we’ll be updating the content type of the document and the title.
Remember this is a JavaScript code running as a SharePoint 2013 app.
Here is the code on how to do that:
var library;
…. code to get a hold of your library …
var listItemCol;
var clientContext = new SP.ClientContext(‘/’);
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(
‘<View><Query><Where><Contains><FieldRef Name=’FileLeafRef’/>’ +
‘<Value Type=’Text’>’+itemName+'</Value></Contains></Where></Query>’ +
‘<RowLimit>10</RowLimit></View>’
);
listItemCol = library.getItems(camlQuery);
clientContext.load(listItemCol);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.updateItemProperties),
Function.createDelegate(this, this.genericFail)
);
}
function updateItemProperties(sender, args) {
var listItemEnumerator = listItemCol.getEnumerator();
var listItem;
while (listItemEnumerator.moveNext()) {
listItem = listItemEnumerator.get_current();
break;
}
// this is an example of updating the content type of a list item
listItem.set_item(‘ContentType’, “Document”);
// here is how to update the title
listItem.set_item(‘Title’, “My new title”);
listItem.update();
clientContext.load(listItem);
clientContext.executeQueryAsync();
}
Check out Yaroslav Pentsarskyy blog ShareMuch for more insightfull content. Check out our resource centre for more SharePoint content from Yaroslav and other SharePoint specialists!