views:

3218

answers:

2

I've developed a custom web part. I would like to create a feature that when activated via STSADM adds the web part to the default page of a WSS Site. Hopefully this can be done without writing code.

This webpart is meant to de deployed to the web application's bin directory and featurereceiver must be installed to the GAC. The AllUsersWebPart element looks promising, but I can't figure out how to get it to work.

TIA,

jt

+1  A: 

The AllUsersWebPart element is meant to be used in a site definition, AFAIK it has no relevance in a feature definition.

The easiest way to do this that comes to mind involves some code, but a minimal amount. Write your feature to deploy the webpart, and add a feature receiver to the feature, utilizing the FeatureActivated method to get an instance of the default.aspx page, and use the SPLimitedWebPartManager to add the webpart to the page.

Something like

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWeb activatedWeb = (SPWeb)properites.Feature.Parent; 
    SPFile defaultPage = activatedWeb.GetFile("Pages/default.aspx"); 
    SPLimitedWebPartManager manager = defaultPage.GetLimitedWebPartManager(PersonalizationScope.Shared);

    MyNewWebPart webPartToAdd = new MyNewWebPart();
    //Initialize your webpart however you may need to

    manager.AddWebPart(webPartToAdd, "Zone 1", 1); //Replace with proper webpartzone name and index;

    defaultPage.Publish();
    defaultPage.Approve();
}
OedipusPrime
the code as written doesn't work, the Publish() method requires a string parameter. also, my requirements are for a WSS site not a publishing site so I don't need to do either of those steps.as I stated, i don't want to do it in code as this is scoped to a web app and i can't deploy to the GAC
Jason
+3  A: 

The AllUsersWebPart tag allow us to insert one webpart and place it where we want using WebPartOrder and WebPartZone attributes. The content of the tag is the dwp (or webpart) of your webpart. For example, for a dwp:

<AllUsersWebPart WebPartZoneID="MiddleLeftZone" WebPartOrder="0">
    <![CDATA[                                              
      <WebPart xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/WebPart/v2"&gt;
        <FrameType>None</FrameType>
        <AllowMinimize>true</AllowMinimize>
        <IsVisible>true</IsVisible>
        <Assembly>Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
        <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>
        <ContentLink xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
        <Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor"&gt;
          <Value>&lt;iframe src="$Resources:Summary_Url;" frameborder="0" scrolling="no" width="100%" height="100%"&gt;&lt;/iframe&gt;</Value>
        </Content>
        <PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
    </WebPart>
    ]]>
  </AllUsersWebPart>

For a webpart:

<AllUsersWebPart WebPartZoneID="MiddleLeftZone" WebPartOrder="1">
    <![CDATA[                                              
      <webParts>
        <webPart xmlns="http://schemas.microsoft.com/WebPart/v3"&gt;
          <metaData>
            <type name="MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f3b9d1137704f880" />
            <importErrorMessage>No se puede importar este elemento Web.</importErrorMessage>
          </metaData>
          <data>
            <properties>
              <property name="AllowClose" type="bool">False</property>
              <property name="AllowMinimize" type="bool">False</property>
              <property name="AllowConnect" type="bool">True</property>
              <property name="ChromeType" type="chrometype">None</property>
              <property name="Hidden" type="bool">False</property>
              <property name="AllowEdit" type="bool">False</property>
              <property name="AllowZoneChange" type="bool">False</property>
              <property name="MissingAssembly" type="string">No se puede importar este elemento Web.</property>
              <property name="ChromeState" type="chromestate">Normal</property>
            </properties>
          </data>
        </webPart>
      </webParts>
    ]]>
  </AllUsersWebPart>

I haven't tried before but I think it can be done once the web has been created. Try it and tell us.

jaloplo
i tried with no luck in a feature's elementmanifest file.
Jason
These two examples have been taken from a Feature I made to a customer. The first one adds a content webpart that have an iframe with a source url readed from the resource file of the feature. The second one is webpart of my own, the only thing you would have to change is the assembly declaration.
jaloplo
@jaloplo Thank you a bunch! I was having difficulty getting a custom web part of my own working, and never knew that .dwp and .webpart were handled differently in the schema. Thanks to finding your post, I was able to fix this issue!
ccomet