Hi,
I have this error "Exception has been thrown by the target of an invocation." when I execute the following code form my sharepoint hosted app. I think it's an security access error, but the app have full control access on the site collection.
var clientContext = new SP.ClientContext(_spPageContextInfo.webAbsoluteUrl); var factory = new SP.ProxyWebRequestExecutorFactory(_spPageContextInfo.webAbsoluteUrl); clientContext.set_webRequestExecutorFactory(factory); var clientContextSite = new SP.AppContextSite(clientContext, _spPageContextInfo.siteAbsoluteUrl); var oFile = clientContextSite.get_web().getFileByServerRelativeUrl(PageUrl); var limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared); var webPart = limitedWebPartManager.get_webParts().getById(SPWebPartID); webPart.get_webPart().get_properties().set_item("JSLink", "[url]"); webPart.saveWebPartChanges(); clientContext.executeQueryAsync(Function.createDelegate(this, function () { //Done }), Function.createDelegate(this, function () { //TODO }));
Note: I used the same code without ProxyWebRequestExecutorFactory, and run direct on the site collection home page, and it is work without any issue:
function UpdateWPProp () { var clientContext = new SP.ClientContext(); var oFile = clientContext.get_web().getFileByServerRelativeUrl((PageUrl);); var limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared); var webPart = limitedWebPartManager.get_webParts().getById("SPWebPartID"); webPart.get_webPart().get_properties().set_item("JSLink", "[URL]"); webPart.saveWebPartChanges(); clientContext.executeQueryAsync(Function.createDelegate(this, function () { //Done }), Function.createDelegate(this, function () { //Error })); };
any help would be appreciated