Cannot add custom web part to page using the JS OM

Daniel McPherson
  • Cannot add custom web part to page using the JS OM Daniel McPherson

    I'm trying to add a custom web part (that is one I have written myself) using the new Javascipt OM available with SharePoint 2010. My solution is also running within the Sandbox.

    I have it working correctly for the Content Editor Web Part (based on sample code), however when I replace the Content Editor Web Part definition with one for my own web part I get the following error:

    Message from webpage
    Request failed. The operation could not be completed because the Web Part is not on this page.

    Things I have tested:

    1. I can successfully upload my .webpart definition to the web part gallery, and the web part works.
    2. I can successfully add the web part to the page where I'm programmatically trying to add it using the "Upload Web Part" feature.

    This is the code I'm using to add the web part:

    function addWebPart(serverRelativeUrl) {
    //var webPartXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
    //webPartXml = webPartXml + "<WebPart xmlns:xsi=\"\" xmlns:xsd=\"\" xmlns=\"\">  <Title>Content Editor</Title>  <FrameType>Default</FrameType>  <Description>Allows authors to enter rich text content.</Description>  <IsIncluded>true</IsIncluded>  <ZoneID>Main</ZoneID>  <PartOrder>0</PartOrder>  <FrameState>Normal</FrameState>  <Height />  <Width />  <AllowRemove>true</AllowRemove>  <AllowZoneChange>true</AllowZoneChange>  <AllowMinimize>true</AllowMinimize>  <AllowConnect>true</AllowConnect>  <AllowEdit>true</AllowEdit>  <AllowHide>true</AllowHide>  <IsVisible>true</IsVisible>  <DetailLink />  <HelpLink />  <HelpMode>Modeless</HelpMode>  <Dir>Default</Dir>  <PartImageSmall />  <MissingAssembly>Cannot import this Web Part.</MissingAssembly>  <PartImageLarge>/_layouts/images/mscontl.gif</PartImageLarge>  <IsIncludedFilter />  <Assembly>Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>  <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>  <ContentLink xmlns=\"\" />  <Content xmlns=\"\" />  <PartStorage xmlns=\"\" /></WebPart>";
    var webPartXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
           "<webParts><webPart xmlns=\"\">" +
           "<metaData>" + 
           "<type name=\"Codezeven.Social.Views.WebParts.ItemViewTrackerWebPart, Codezeven.Social.Views, Version=, Culture=neutral, PublicKeyToken=c56ab109c2917056\" />" +
           "<importErrorMessage>Cannot import this Web Part.</importErrorMessage>" +
           "<Solution SolutionId=\"f5984dff-f7c0-4108-8c08-671bbb9b5df0\" xmlns=\"\" />" +
           "</metaData>" +
           "<data><properties>" +
           "<property name=\"Title\" type=\"string\">ItemViewTrackerWebPart</property>" +
           "</properties></data>" + 
    clientContext = new SP.ClientContext.get_current();
    var oFile = clientContext.get_web().getFileByServerRelativeUrl(serverRelativeUrl);
    var limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
    var oWebPartDefinition = limitedWebPartManager.importWebPart(webPartXml);
    this.oWebPart = oWebPartDefinition.get_webPart();
    limitedWebPartManager.addWebPart(oWebPart, 'Main', 0);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));}
    function onQuerySucceeded() {
        alert('Web Part added: ' + oWebPart.get_title());
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());

    UPDATE: Based on feedback from Wictor, I tried the following:

    <?xml version="1.0" encoding="utf-8"?>
      <webPart xmlns="">
          <type name="Microsoft.SharePoint.WebPartPages.SPUserCodeWebPart, Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
          <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
            <property name="Title" type="string">ItemViewTrackerWebPart</property>
        <property name="SolutionId" type="string">f5984dff-f7c0-4108-8c08-671bbb9b5df0</property>
        <property name="AssemblyFullName" type="string">Codezeven.Social.Views, Version=, Culture=neutral, PublicKeyToken=c56ab109c2917056</property>
        <property name="TypeFullName" type="string">Codezeven.Social.Views.WebParts.ItemViewTrackerWebPart</property>
        <property name="Description" type="string">ViewTracker</property>

    On this I'm getting a "Cannot Import Web Part" error. I think this is heading in the right direction, not sure if I have an issue in my .webpart file.

  • Try to add it using the SPUserCodeWebPart instead and pass the AssemblyFullName, TypeFullName and SolutionId parameters instead.

Related questions and answers
  • " /&gt; &lt;/ElementManifests> &lt;/Feature> elements.xml &lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;Elements xmlns=""&gt; &lt;/Elements>... in a WebPart zone. When doing this, I get an error message : Tooltips: List View Web Part could not be added, list may be hidden. What's wrong with the list? feature.xml &lt;?xml version="1.0..., Culture=neutral, PublicKeyToken=1de0ffdb1fdf1b80" ReceiverClass="MyApp.WebServices.TooltipsReceiver" xmlns=""&gt; &lt;ElementManifests> &lt

  • ;?xml version="1.0" encoding="utf-8" ?&gt; &lt;Elements xmlns=""&gt; &lt;Module Name="WebPartPopulation" Url="_catalogs/wp" RootWebOnly="TRUE"&gt...; } } } } finally { if (site != null) site.Dispose(); } } In the feature.xml i have this below &lt;?xml version="1.0" encoding="utf-8...I have a webpart i build using WSpBuilder and it works OK. When i use Deploy from Visual Studio it installs the WSP sharepoint and add the webpart to the webpart gallery. But it seems the feature

  • someone else has cracked this nut. &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;Elements xmlns=""&gt; &lt;Module Name="Pages"&gt; &lt;File Path="" Url...://"&gt; &lt;Assembly>(Assembly), Version=, Culture=neutral, PublicKeyToken=12baeb5f1600543c&lt;/Assembly> &lt;TypeName>...I've been hitting my head against a wall on this one for the last couple of days. I have a pretty simple requirement but I cant see how it can be achieved within a Sandboxed solution. I'm depressed

  • (I have also asked this question on Stack Overflow) I have a SharePoint feature I'm using to add some custom aspx files to the Pages Library. When I activate the feature, I can visit the pages in-browser and they are visible in SPDesigner, but when I "View All Site Content" they are not there. Why is this the case? The Feature's Elements File: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;Elements Id="9e85eb79-6d8d-4ff3-b0d4-64d55c3bb577" xmlns=""&gt; &lt;Module Name="Pages" Url="Pages"&gt; &lt;File Path="Example.aspx" Url

  • ;Property Name="Group" Value="Custom" /&gt; &lt;/File> &lt;/Module> &lt;/Elements> IdeaEditor elements.xml &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;Elements xmlns="http... to be listed under the custom group. Any ideas on how I can debug this? Any constructive advice is greatly appreciated. Home elements.xml &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;Elements xmlns...="GhostableInLibrary" &gt; &lt;Property Name="Group" Value="Custom" /&gt; &lt;/File> &lt;/Module> &lt;/Elements> IdeaGrid elements.xml &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;Elements

  • information using GetList. I use the List GUID as the list name. An exception is thrown. The SOAP body is shown below. &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;soap:Envelope xmlns:soap="http...I'm have some trouble using the GetList web service. It does work sometimes but not at the start of the day!?! Here is my code: public static void ListSurveys(string serverName...;errorstring xmlns=""&gt; Die Liste ist nicht vorhanden. Die ausgewählte Seite enthält eine Liste, die nicht vorhanden ist. Die

  • confirm (or deny) that the following looks legitimate (or suggest a better way to do it)? &lt;xsl:stylesheet xmlns:x="" xmlns:d=" anybody upgraded an old custom field type to use the new XSL transform in SharePoint 2010? I have a multi-column field type, and want the Display pattern to render the 2nd "column", which...;/RenderPattern> In WSS 3.0, this would render the 2nd column in the field as a hyperlink in the LVWP, which is what I want. In SharePoint 2010, I tried adding TRUE to my field type definition

  • ;xsl:stylesheet xmlns:x="" xmlns:dsp="" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http...Hello I really need help with this... , I am using Sharepoint 2007. I have the 'collect data from user' action in a workflow - which generates a custom form within the workflow folder. The form... have tried adding a content editor webpart and used jquery to update a a form field value when the button is clicked as such $(document).ready(function (){ $("input[type=button][value='Complete Task

  • the 'ListForm' it works. So I'm thinking that I've done something wrong in the content type definition. Here is the code: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;Elements xmlns=""&gt; &lt;Field ID="{9381E2BE-CE62-4673-9C0D-8A7E4B087B55}" Name="MyPal" DisplayName="My Pal" Type="Text" /&gt; &lt;!-- Parent ContentType: Item (0x01) --&gt; &lt...;%@Assembly Name="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %&gt; &lt;%@Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=

Data information