2
votes

From code I've automatically created a lot of similar sites (SPWeb) in my site collection from a site template (in Sharepoint Foundation). Every site has a home page on which I've added the "what's new" web part (found under "Social collaboration").

Even though the web part has several "target lists" (I'd have called it "source lists") added to it on the template site, this connection is lost on the sites created from the template. So I need to programmatically find all these web parts and add the target lists to them. Looping the web parts is not an issue - I've done that before - but I can't seem to find a word on the net on how to go about modifying this particular web part. All I have is a brief intellisense.

I've found out that it recides in the

Microsoft.SharePoint.Applications.GroupBoard.WebPartPages 

namespace, but on the lists provided on MSDN this is one of very few namespaces that doesn't have a link to a reference documentation.

Does anyone have any experience of modifying this web part from code? If not, how would you go about to find out? I can't seem to figure out a method for this..

2

2 Answers

3
votes

Here is how I did it. It worked really well. I had a feature that created several list instances and provisioned the What's New web part. In the Feature Receiver, I looped through all of the list instances, indexed the Modified field, and then added the list to the web part:

private void ConfigureLists(SPWeb web, SPFeatureReceiverProperties properties)
{
    List<Guid> ids = new List<Guid>();
    SPElementDefinitionCollection elements = 
        properties.Feature.Definition.GetElementDefinitions(new CultureInfo((int)web.Language, false));
    foreach (SPElementDefinition element in elements)
    {
        if ("ListInstance" == element.ElementType)
        {
            XmlNode node = element.XmlDefinition;
            SPList list = web.Lists[node.Attributes["Title"].Value];
            SPField field = list.Fields[SPBuiltInFieldId.Modified];
            if (!field.Indexed)
            {
                field.Indexed = true;
                field.Update();
            }
            ids.Add(list.ID);
        }
    }

    string targetConfig = string.Empty;
    foreach (Guid id in ids)
    {
        targetConfig += string.Format("'{0}',''\n", id);
    }
    SPFile file = web.GetFile("Pages/default.aspx");
    file.CheckOut();
    using (SPLimitedWebPartManager manager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
    {
        WhatsNewWebPart webpart = null;
        foreach (System.Web.UI.WebControls.WebParts.WebPart eachWebPart in manager.WebParts)
        {
            webpart = eachWebPart as WhatsNewWebPart;
            if (null != webpart)
            {
                break;
            }
        }
        if (null != webpart)
        {
            webpart.TargetConfig = targetConfig;
            manager.SaveChanges(webpart);
        }
    }
    file.CheckIn("ConfigureWebParts");
    file.Publish("ConfigureWebParts");
    file.Approve("ConfigureWebParts");
}
1
votes

If you are unsure about the property, export the web part from the browser, then open the .webpart/.dwp file with a text editor. Somewhere in the xml will be a reference to the source list.

*.webparts are usually easier to modify, just set the property.

*.dwps are harder because you sometimes have to get the property (eg ViewXML), then load it into an XmlDocument, then replace the property, and write the xml document string value back to ViewXML.