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.
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.
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
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.