MOSS: Programmatically modifying a shared webpart

On my current project, we create SharePoint sites in response to calls to a particular webservice and there is one site per domain entity.  In this webservice, there is a certain amount of customisation of the site homepage which needs to be done.  In particular, we have an RSS Aggregator web part and we want this to point to a different URL for each site.

I couldn't find anywhere on the web that had a sample of doing this, so I thought I'd blog the solution:

// Reconfigure the RSS Reader to query against the company name

SPLimitedWebPartManager WPMgr = NewSite.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared);

foreach (WebPart wp in WPMgr.WebParts)

{

    RSSAggregatorWebPart RssPart = wp as RSSAggregatorWebPart;

    if (RssPart != null)

    {

        string QueryString = HttpContext.Current.Server.UrlEncode(person.Company);

        RssPart.FeedUrl = RssPart.FeedUrl.Replace("company", QueryString);

        WPMgr.SaveChanges(RssPart);

    }

}

 

Clearly NewSite and person.Company are defined higher up in my code, but you can do whatever you want with the FeedUrl property.  To get this to work you need to have references to:

  • System.Web [for System.Web.WebPart and System.Web.UI.WebControls.WebParts.PersonalizationScope]
  • Microsoft.SharePoint [for Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager]
  • Microsoft.SharePoint.Portal [for Microsoft.SharePoint.Portal.WebControls.RSSAggregatorWebPart]

The list above also shows which namespaces I import through using declarations.

PS. Apologies to anyone who's disappointed at the lack of WPF/E content in my latest posts.  I hope to play some more with that technology soon, but there'll probably be a pre-dominance of MOSS posts in the coming months.  If you are really uninterested in MOSS posts, then make sure you only subscribe to a tagged feed and not to the main one.

January 22 2007
blog comments powered by Disqus