12

I've been looking for a way to add a web part to a page using JSOM/CSOM or any other api available to sharepoint hosted apps.

The only solutions I have found so far are for custom sharepoint solutions. Could anyone confirm that this is (im)possible?

To be clear, I want to add a web part to a sharepoint page. Not to a page of my app.

9
  • If you give access to the library that holds the web part page, then you could probably edit it through jsom, and then add web part. Heh, if you could do that you could actually edit the iframe tag that holds your app, and through html5 (sandbox allow attribute) give full access, which renders the DNS-solution for cross origin lockdown useless.. so perhaps not
    – eirikb
    Commented Feb 26, 2014 at 15:16
  • is this what you are looking for : sharepoint.stackexchange.com/questions/79579/… ? Commented Feb 26, 2014 at 15:44
  • @CameronVerhelst that answer is csom, not jsom. And, for what it is worth, missing the auth, but could probably be converted to jsom. This might be interesting though: sharepoint.stackexchange.com/a/86569/627
    – eirikb
    Commented Feb 26, 2014 at 15:47
  • 4
    I realise this, but the question does ask for either JSOM/CSOM Commented Feb 26, 2014 at 15:49
  • 2
    If anyone is interested, I have found something interesting myself in the meantime at msdn.microsoft.com/en-us/library/office/… Commented Feb 26, 2014 at 15:56

1 Answer 1

15

How to add WebPart client object on page via JSOM

function addWebPart(webUrl, pageUrl,webPartXml,zoneId,zoneIndex, Success,Error){
    var context = new SP.ClientContext(webUrl);
    var web = context.get_web();

    var file = web.getFileByServerRelativeUrl(webUrl + pageUrl);
    var webPartMngr = file.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
    var webPartDef = webPartMngr.importWebPart(webPartXml);
    var webPart = webPartDef.get_webPart();
    webPartMngr.addWebPart(webPart, zoneId, zoneIndex);

    context.load(webPart);
    context.executeQueryAsync(
      function() {
        Success(webPart);
      },
      Error
    );
}

Example: add Content Editor on root web

var webPartXml = '<?xml version="1.0" encoding="utf-8"?>' +
'<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">' +
    '<Assembly>Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>' + 
    '<TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>' + 
    '<Title>$Resources:core,ContentEditorWebPartTitle;</Title>' +
    '<Description>$Resources:core,ContentEditorWebPartDescription;</Description>' +
    '<PartImageLarge>/_layouts/15/images/mscontl.gif</PartImageLarge>' +
'</WebPart>';

addWebPart('/','Pages/default.aspx',webPartXml,'Left',1,function(webPart){
    console.log(webPart.get_title() + ' has been added'); 
},function(sender,args){
    console.log(args.get_message());
});
3
  • 1
    Nice answer (+1). Perhaps send in context and web, since you would need to call set_webRequestExecutorFactory? Also, I tried to do this with REST earlier before your post, but I wasn't able to call addWebPart, I just got 404, any idea why? (importWebPart worked fine, at least I got some kind of reply)
    – eirikb
    Commented Feb 26, 2014 at 19:20
  • Thanks! Good idea, but I have not found a way to add web part on page via REST yet. Commented Mar 3, 2014 at 11:00
  • 2
    "I wasn't able to call addWebPart, I just got 404, any idea why?" The AddWebPart_Client method of the SPLimitedWebPartManager class is the client-side equivalent of the AddWebPart method. Unfortunately, this method is decorated with a ClientCallableMethod attribute having ClientLibraryTargets=ClientLibraryTargets.NonRESTful, making this method unusable from REST calls.
    – pholpar
    Commented Jul 22, 2016 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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