SharePoint 2010: How to Retrieve a Personalized Web Part Property for All Users


I have a built-in SharePoint 2010 Web Part that inherits from System.Web.UI.WebControls.WebParts.WebPart

that has a property defined like this:

public string CustomProp { get; set; }


The Powers-That-Be would like to know the cost of a property for each of its 3,000 users. I am already familiar with how to use SPLimitedWebPartManager to retrieve a property in a shared view or my personal view using code similar to:

var pageUrl = "/Pages/SomePage.aspx";
var limitedManager = SPContext.Current.Web.GetLimitedWebPartManager(
    PersonalizationScope.User); // or .Shared, if loading the shared value
var webPart = limitedManager.WebParts.OfType<MyWebPart>().FirstOrDefault();
var prop = webPart.CustomProp;


So the question is:

I am going to collect this information for all users . Assuming I already have, or know how to get, a list of logins or SPUser objects, how can I find the web part property for all users? I can use suggestions in C #, VB.NET, or PowerShell.


source to share

1 answer

Try this code - I haven't tested it, but I think it should work.

const string absolutePageUrl = "http://spsite:5000/Pages/SomePage.aspx";

foreach (SPUser user in SPContext.Current.Site.RootWeb.AllUsers.Cast<SPUser>())
    if (!user.LoginName.StartsWith("DOMAIN NAME"))

    using (SPSite site = new SPSite(absolutePageUrl, user.UserToken))
    using (SPWeb web = site.OpenWeb())
        var mgr = web.GetLimitedWebPartManager(absolutePageUrl, PersonalizationScope.User);
        var webPart = mgr.WebParts.OfType<MyWebPart>().FirstOrDefault();
        var prop = webPart.CustomProp;




All Articles