Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebPart "metadata"?

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.

like image 574
epitka Avatar asked Apr 19 '26 01:04

epitka


1 Answers

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();
        }
    }
}
like image 130
Alex Angas Avatar answered Apr 21 '26 07:04

Alex Angas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!