3

I know its possible to create a wiki page in a sandbox solution with this:

SPUtility.CreateNewWikiPage(list, "{mysiteurl}/CodeGeneratedPage.aspx");

But how can I (if possible) create a webpart page with included webpart in my document library? Because all I can find (like this) is only working in farm solutions. Is there a workaround somehow?

Notice: I post this question here because there are way more people here as on the Sharepoint site.

Community
  • 1
  • 1
Marc
  • 6,749
  • 9
  • 47
  • 78

1 Answers1

2

Good find. You cannot use the SPLimitedWebPartManager in sandboxed code and hence can't use it to put WebParts on pages.

The only way to provision WebParts to pages is the declarative way (i.e. XML), so you will have to do something like this in an elements.xml file:

  <AllUsersWebPart WebPartZoneID="MainWebPartZone" WebPartOrder="1">
    <![CDATA[
      <webParts>
        <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
          <metaData>
            <type name="MyComp.WebParts.SampleWebPart, MyComp.WebParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5" />
            <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
          </metaData>
          <data>
            <properties>
              <property name="Title" type="string">Sample WebPart</property>
            </properties>
          </data>
        </webPart>
      </webParts>
    ]]>       
  </AllUsersWebPart>

These links will help you along your way:


and some more:

Dennis G
  • 21,405
  • 19
  • 96
  • 133
  • 1
    Thanks a lot for your answer, but I have to create the webpart page programmatically (not at deploy time) when a user does whatever. Maybe there is a workaround somehow to execute some .net/sharepoint method which would create the page at deploy time from the XML? – Marc Apr 24 '12 at 10:42
  • I added two more links which might help - but I don't know of any way of doing this in code. You will need to use the XML to provision the stuff as SharePoint uses the `SPLimitedWebPartManager` in the background when deploying - you just can't use it in your code. – Dennis G Apr 25 '12 at 10:01