简体   繁体   中英

Sharepoint - upgrade webpart properties

I'm playing with SharePoint 2010 now and have a problem. There is a feature that is responsible for webparts - it's scope is web. It's needed to update some properties of already created webparts, for example - title.

So I've overridden FeatureUpgrading event and added custom upgrade action into feature manifest - there is no problem here. In that feature receiver I plan to have a code that should get the file with needed page, check it out, iterate through all the web parts on it, change property and then check in page back. The problem is that all my webparts appear as ErrorWebPart with empty title.

By the way, if I use the same code in FeatureDeactivating event - everything works good. The only difference I've noticed - in featureupgrading HttpContext.Current was null. So I've populated it manually but it didn't help. While googling, the only two advices were: populate HttpContext and ensure that libs with webparts are in GAC. Both conditions are done in my situation.

The sample code from FeatureUpgrading is as proper:

 SPUtility.ValidateFormDigest();
        web.AllowUnsafeUpdates = true;

        var request = new HttpRequest("", web.Url, "");
        HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter()));
        HttpContext.Current.Items["HttpHandlerSPWeb"] = web;

        var fileUrl = web.Url + "/pages/sample.aspx";
        var file = web.GetFile(fileUrl);
        if (file.CheckOutType == SPFile.SPCheckOutType.None)
        {
            file.CheckOut();
        }

        using (var webPartsManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
        {
            foreach (WebPart webPart in webPartsManager.WebParts)
            {
                Console.WriteLine(webPart.GetType().ToString());
            }
        }

        file.CheckIn("System update");

Would appreciate any help. Maybe there is something I'm missing or there is another variant to accomplish described task?

Regards.

Since it is working on Deactivating(), I think the order in which the features are activated is the problem.
You may have to ensure the below:
1. First activate the Feature that adds the webparts
2. Then activate the new feature.

You can change this in package. Or better add a dependency to your new feature.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM