I'm trying to add a webpart(sandboxed) to a page programmatically using CSOM.
I've created a simple windows forms application and tried the below, it gives me error
A first chance exception of type 'Microsoft.SharePoint.Client.ServerException' occurred in Microsoft.SharePoint.Client.Runtime.dll"- Server Exception occurred. Additional Information: Cannot Import this webpart
using (ClientContext context = new ClientContext("URL"))
{
Web web = context.Web;
File file = web.GetFileByServerRelativeUrl("/SitePages/Test.aspx");
LimitedWebPartManager wpMgr = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
string webpartxml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<webParts>" +
"<webPart xmlns='http://ift.tt/1fI5tWY'>" +
"<metaData>" +
"<type name=\"CustomVisualWebPartProject.VisualWebPart1.VisualWebPart1, CustomVisualWebPartProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=28942db35e131a1c\" />" +
"<importErrorMessage>Cannot Import this webpart</importErrorMessage>" +
"</metaData>"+
"<data>"+
"<properties>"+
"<property name=\"Title\" type=\"string\">CustomVisualWebPartProject - VisualWebPart1</property> "+
"<property name=\"Description\" type=\"string\">My Visual Web Part</property>" +
"<property name=\"ChromeType\" type=\"chrometype\">None</property>" +
"</properties>" +
"</data>" +
"</webPart></webParts>";
WebPartDefinition webPartDef = wpMgr.ImportWebPart(webpartxml);
wpMgr.AddWebPart(webPartDef.WebPart, "Left", 1);
context.ExecuteQuery();
}
Please let me know how to fix this error.
0 commentaires:
Enregistrer un commentaire