2

I have found loads of guides showing how to do this, but none if them show how to do it using the client side model.

This should do what i want, but its using the server side model and not the client side.

How to programmatically add a webpart to a page?

5

Yes, you can add webparts using the client side object model.

Summary: Use the LimitedWebPartManager defined for C# or JavaScript.


Adding webparts is generally considered part of a provisioning solution and the OfficeDev Patterns and Practices (PnP) repository on GitHub has many examples that demonstrate different provisioning techniques.

One specific scenario that demonstrates inserting a webpart from an add-in into a hostweb is called Core.AppScriptPart, you'll find the repository folder for it at https://github.com/OfficeDev/PnP/tree/master/Samples/Core.AppScriptPart

Andrew Connell created a video hosted on Channel9 that describes the solution and it's technique as well as the PnP program in general.

This example uses a plain old Script webpart that is modified to point to a specific Javascript source file, but you can modify the webpart or the Javascript used in this technique to solve many different scenarios.

To see the nuts and bolts of how the solution works, navigate to the code for Pages/Default.aspx.cs

This solution uses a .webpart file loaded into SharePoint as part of the add-in (the file is contained in the solution) then uses the CSOM to upload that file into the host web's Web Part Gallery list.

There are also other solutions that demonstrate how to add webparts to pages using the LimitedWebPartManager which is available in the CSOM, you can find that example in /Samples/Core.UploadWebPart/Core.UploadWebPartWeb/Pages/Default.aspx.cs in the function on line 84 called btnAddToPage_Click

-2

You not can add webpart with client side object model because the client side object model not have Farm scope. For to do this you need Farm scope.

  • is it possible to replace a page with another one? If i make a "template" page that i can manually create, can i use this page as a template and replace the other home pages with this one? – Dan Hastings Sep 18 '15 at 15:08
  • 2
    This answer is generally incorrect; there are limits, but adding webparts is certainly possible even using the Javascript version of the client object model. – John-M Sep 18 '15 at 15:08
  • do you know how to do it? – Dan Hastings Sep 18 '15 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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