Question

Background:

I have a built-in-house custom SharePoint 2010 web part, inheriting from System.Web.UI.WebControls.WebParts.WebPart, which has a property defined like so:

[Personalizable(PersonalizationScope.User)]
[WebBrowsable(true)]
[WebDisplayName("...")]
[WebDescription("...")]
[Category("...")]
public string CustomProp { get; set; }

The Powers-That-Be would like to know the value of the property for each of its 3,000 users. I'm already familiar with how to use the SPLimitedWebPartManager to extract the property in either shared view or my own personal view, using code similar to:

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

So, the question:

I'm stuck on collecting this information for all users. Assuming I already have, or know how to retrieve, a list of login names or SPUser objects, how do I go about retrieving the web part property for all users? I can use suggestions in C#, VB.NET, or PowerShell.

Was it helpful?

Solution

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"))
        continue;

    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;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top