3

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.

kbrimington
  • 25,142
  • 5
  • 62
  • 74

1 Answers1

2

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;
    }
}
asdfjklqwer
  • 3,536
  • 21
  • 19
  • Thanks, Nathan! This put me on the right track. I rolled a solution that involved direct SQL access, but this got me going using the supported API! A few things, and I'll upvote and mark as correct: swap `limitedManager` for `mgr`, and modify the `new SPSite()` call to use the site Guid (no constructor exists for `(string, usertoken)`). I'll post PowerShell adaptations of this code and of my direct-SQL approach, the the mark-as-answer is yours once the code syntax is corrected. – kbrimington Mar 14 '12 at 17:16
  • Ah, thanks for pointing that out - I've renamed limitedManager. That SPSite constructor does exist (http://msdn.microsoft.com/en-us/library/ms469253.aspx) but it requires an absolute URL (so I've updated accordingly). – asdfjklqwer Mar 16 '12 at 00:45