3
votes

I have not worked with webparts for sharepoint before, but need to make change to a webpart, that needs to be propagated to some 700 websites. It is a change to one of the properties of the webpart, the value needs to be changed. Is there a way to get metadata for a webpart and change it directly in the database (I assume that is where it is stored.)?

Here is the scenario: Webpart contains a comma delimited list of document types (internal type) that it should display. Now there are new doc. types that need to be added to all 700 websites. I need a way to enumerate websites, get the webpart metadata, and add these new doc types to webpart. Currently they go manually to each website, click on edit, type in new doc type, and save it.

3

3 Answers

3
votes

Directly accessing the sharepoint content databases is a big "no no." That's the official answer. :)

That being said, I have only ever looked in the content databases and never tried to actually change anything manually.

My suggestion, would be to modify the existing web part to modify the property based on currently set property(s). (I am assuming that some currently set property is invalid or needs to be updated based on changes to the infrastructure.) ... If this is the case, you can validate the property; making sure that current property is changed to what it needs to be, and/or making sure future property changes are valid.

Good luck!

3
votes

DON'T

Seriously, do not go into the content databases and edit it. That way you are not supported anymore if anything should happen and Microsoft will not support you anymore (not until you revert the database back to an earlier version from a backup that is).

You can use the API to access webparts in your sites, here's some code that should get you started:

Enumerate page webparts

3
votes

As others have said the correct approach is to programmatically achieve this rather than edit the content database which will make your installation unsupportable. I regularly use a console application to do this in a site collection made up of sites created from a site template.

Here is an example that changes the Title property of a ListViewWebPart. Updated to include code for recursive loop. I haven't tested this but it should work.

private static void ProcessSiteCollection(string url)
{
    using (SPSite siteCollection = new SPSite(url))
    {
        SPWeb rootWeb = siteCollection.RootWeb;
        ProcessWebs(rootWeb);
    }
}

private static void ProcessWebs(SPWeb parentWeb)
{
    foreach (SPWeb web in parentWeb.Webs)
    {
        try
        {
            UpdateWebPart(web); // Set web part properties
            ProcessWebs(web);   // Recursively loop through children
        }
        finally
        {
            web.Dispose();
        }
    }
}

private static void UpdateWebPart(SPWeb web)
{
    using (SPLimitedWebPartManager webPartManager =
        web.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared))
    {
        try
        {
            foreach (WebPart webPart in webPartManager.WebParts)
            {
                    if (webPart.Title == "My Web Part")
                    {
                            ListViewWebPart listViewWebPart = (ListViewWebPart)webPart;
                            listViewWebPart.Title = "Updated Web Part";
                            webPartManager.SaveChanges(listViewWebPart);
                            web.Update();
                            break;
                    }
            }
        }
        finally
        {
            webPartManager.Web.Dispose();
        }
    }
}