Deploy a web part to a page during feature activation within a Sandbox Solution

Daniel McPherson
  • Deploy a web part to a page during feature activation within a Sandbox Solution Daniel McPherson

    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 because I think its a critical limitation that will cause me grief in future solutions too.

    Here is the scenario.

    I have an existing site and I want to be able to activate a feature, and then as part of that activation, drop web parts onto some specific pages. In my case I want to be able to drop it onto the "AllItems.aspx" page of a list, but I'm equally having trouble adding it to the default.aspx home page. I would be happy either way.

    Note: I want to be able to activate this on existing site, I dont want to create a new Site Definition.

    Here is what I have tried:

    1. Feature Receiver: No go. You cant use the SPLimitedWebPartManager class, and the SPWebPartManager class works only within the context of the page where its instantiated. So bascially, you cant do this in code.

    2. XML Module: I think this is the most likely path forward, however it seems to always want to provision a file to the site. For example, if I want to add a web part to the home page (default.aspx), then there doesn't seem to be a way to just get a reference to the page, it always wants to overwrite it with a new file. But I dont want to do that, I just want to add a Web Part to the existing page. See my elements XML below, I have tried various combinations of "IgnoreIfAlreadyExists" and removing the Path attribute, and setting it blank.

    3. Delegate Control: Not allowed in the sandbox.

    4. I actually want to be able to add a web part to the "AllItems.aspx" form of a list, so I also tried to add the web part via the SCHEMA.XML file as described here: Add Web Part through the SCHEMA.XML but this didn't work either. I should be able to do it by more heavily customising the List Definition, but that seems a little over the top and I'm reluctant to do that before ruling out the other options.

    So, the only Neat way I can see through it is some sort of "Configuration" page, which then does it via the Web Services. But this is a pain, with the user having "Activate" the feature and then "Activate" again on my configuration page.

    Anyway, it may just be that I'm missing something obvious. Been a while between XML fiddling on SharePoint for me, and this is my first Sandbox solution. Really hope someone else has cracked this nut.

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <Module Name="Pages">
        <File Path="" Url="default.aspx">
          <AllUsersWebPart WebPartZoneID="Left" WebPartOrder="1">
            <![CDATA[
                  <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" xmlns:iwp="http://schemas.microsoft.com/WebPart/v2/Image">
                    <Assembly>(Assembly), Version=1.0.0.0, Culture=neutral, PublicKeyToken=12baeb5f1600543c</Assembly>
                    <TypeName>(Typename)</TypeName>
                    <FrameType>None</FrameType>
                    <Title>ListAnnouncement</Title>
                  </WebPart>
                ]]>
          </AllUsersWebPart>
        </File>
    </Module>
    </Elements>
    

  • Given that the goal is to have no manual steps, I can think of a rather hackish workaround that may work.

    Use a <CustomAction Location="ScriptLink" ScriptBlock="..."> to 'auto' add script on pages. In the load event handler added in this script, use the client OM to find out about any new Web Parts to add to the page. Next, to find out what Web Parts to add after a feature is activated, use a feature receiver to write a registration to a 'Web Parts to Add' list. In this list, you could add four columns:

    • Page Url
    • Web Part Zone
    • Zone Index
    • Web Part Type

    I do have the feeling that this is a bit of a workaround.

    Big downsides are the fact that with this setup you have to poll the list for new Web Parts each request, or be smart and add some logic to the load script to disable polling to more than once a day (HTTP Cookie for storage).

    As far as I can tell, it'll work in the sandbox though!

    Hope it helps,

    Wouter van Vugt SharePoint HackMaster, but also build sensible SharePoint software... http://blogs.code-counsel.net/wouter

Tags
sandbox-solution web-part
Related questions and answers
  • (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="http://schemas.microsoft.com/sharepoint/"&gt; &lt;Module Name="Pages" Url="Pages"&gt; &lt;File Path="Example.aspx" Url

  • 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... --------------------------- Things I have tested: I can successfully upload my .webpart definition to the web part gallery, and the web part works. I can successfully add the web part to the page...()); } UPDATE: Based on feedback from Wictor, I tried the following: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;webParts> &lt;webPart xmlns="http://schemas.microsoft.com/WebPart/v3"&gt; &lt

  • " /&gt; &lt;/ElementManifests> &lt;/Feature> elements.xml &lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;Elements xmlns="http://schemas.microsoft.com/sharepoint/"&gt; &lt;/Elements>...I've created a Custom List ('Tooltips') in SharePoint. After this I used SPSource to reverse engineer the list into a feature. After installing and activating this feature, I want to add this list 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

  • as expected. When I try to add a new item to the list, the list form only shows the Title field from Item. Now, here's the weird part. If I add a second content type to the list (through the UI), the list form suddenly works correctly. This makes me think there's something wrong with my content type. Here's what I'm working with. Some names have been changed to protect the innocent. &lt;ContentType.... Scoped to Site. Registration: contains a ListTemplate and a ListInstance that just modifies a basic Custom List to enable content types and add my custom content type. Scoped to Web. Here's what's

  • 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="http://schemas.microsoft.com/sharepoint/" &gt; &lt;Module Name="Home" List="113" Url="_catalogs/wp"&gt; &lt;File Path="Home\Home.webpart" Url="Home.webpart" Type="GhostableInLibrary" &gt; &lt... xmlns="http://schemas.microsoft.com/sharepoint/" &gt; &lt;Module Name="IdeaGrid" List="113" Url="_catalogs/wp"&gt; &lt;File Path="IdeaGrid\IdeaGrid.webpart" Url="IdeaGrid.webpart" Type

  • ;?xml version="1.0" encoding="utf-8" ?&gt; &lt;Elements xmlns="http://schemas.microsoft.com/sharepoint/"&gt; &lt;Module Name="WebPartPopulation" Url="_catalogs/wp" RootWebOnly="TRUE"&gt...; &lt;/File> &lt;/Module> &lt;/Elements> But when i go to the site features i can see the feature but when i activate it it does puts the webpart in the gallery but when i...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

  • 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... because my code had fetched the GUID of the list just milliseconds earlier! Can anyone explain what is going on here? I had the same problem yesterday and at some point it just went away...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

  • that is supposed to be the new and improved way of doing this. Unfortunately, I can't get that to work either. I am getting the same thing I got using the RenderPattern. I'm no XSL expert, so can anyone confirm (or deny) that the following looks legitimate (or suggest a better way to do it)? &lt;xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint...;/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

  • to the Pages library. This correctly provisions the page to the Pages library with an Announcements List View web part provisioned to the page. I have about 800 (or so) sites created from this site definition. We have a need to add a few web parts to the page for any new sites created from the template and also for all of the existing 800 sites. The plan was to move the default.aspx module from ONET... existing sties. New sites would pick it up via the feature. When I do this I can deactivate and reactivate the feature to correctly replace the home page with the "latest version". However, when I

Data information