Code Sample: Social Data Statistics Web Part

Applies to: SharePoint Server 2010

The Social Data Statistics Web Part displays social data statistics. This sample is a Microsoft Visual Studio 2010 SharePoint Visual Web Part project. After you build and deploy this project on your Microsoft SharePoint Server 2010 site, you can add this Web Part to any page where you want to display statistics for the social tagging activities of your users. The Web Part displays the following information, in three tables:

  • Each URL that has been tagged, and the terms with which each URL has been tagged

  • Each term that has been used in a social tag, and the number of times that term has been used

  • Each user who has added a social tag, and the number of times that user has tagged URLs

Install this code sample on your own computer by downloading the Microsoft SharePoint 2010 Software Development Kit (SDK) or by downloading the sample from Code Gallery. If you download the SharePoint 2010 SDK, the sample is installed in the following location on your file system: C:\Program Files\Microsoft SDKs\SharePoint 2010\Samples\Social Data and User Profiles.

Retrieving and Storing the Data by Using the Object Model

The Social Data Statistics Web Part creates a UserProfileManager object and a SocialTagManager object by using an SPServiceContext object that you create with a site or site collection that you write directly into your code.

//Change this value to the root URL for your site.
string socialDataStatsSite = "http://mysite";

using (SPSite siteColl = new SPSite(socialDataStatsSite))
{
//Create SocialTagManager with desired SPServiceContext.
SPServiceContext serviceContext = SPServiceContext.GetContext(siteColl);
UserProfileManager myUserProfileManager = new UserProfileManager(serviceContext);
SocialTagManager mySocialTagManager = new SocialTagManager(serviceContext);

Only users listed in the Permissions access control list (ACL) of the User Profile Service Application can create an instance of a UserProfileManager object. To add a user to the Permissions ACL of the User Profile Service Application, perform the steps in the following procedure.

To add a user to the Permissions ACL of the User Profile Service Application

  1. In your browser, go to your SharePoint Central Administration home page.

  2. Navigate to the Manage service applications page, which appears under the Application Management heading.

  3. Select the User Profile Service Application option. Click Permissions.

  4. In the dialog box, add the user accounts. Click OK.

Because social tags use only valid taxonomy terms, the first task that the Web Part must perform is to retrieve all of the social terms that have been created in the current taxonomy store. Each SocialTerm object contains the taxonomy term from the current taxonomy store and a Count property that represents the number of times that term has been used in a social tag.

//Get all social terms and store in an array.
SocialTerm[] socTerms = mySocialTagManager.GetAllTerms();

The next step is to create Dictionary objects that store all of the information that the Web Part displays.

//Create a Dictionary to store values for tags on social URLs, number of times each social term is used,
//and number of times that each user has tagged a URL.
Dictionary<string, string> SocialUrlStats = new Dictionary<string, string>();
Dictionary<string, long> SocialTermStats = new Dictionary<string, long>();
Dictionary<string, int> SocialTagStats = new Dictionary<string, int>();

When the Web Part has retrieved all of the social terms that have been used in social tags, it then iterates through the array of social terms, stores the count for each term, and then retrieves and stores all of the URLs that have been tagged with each term.

//Get a Taxonomy term for each SocialTerm, get the number of times each social term has been used in a tag,
//and get all the URLs for which that term has been used as a tag. Then map each URL to each of the terms with which
//it has been tagged.
foreach (SocialTerm aTerm in socTerms)
{
  string termName = aTerm.Term.Name;
  SocialTermStats.Add(termName, aTerm.Count);
  try
  {
    SocialUrl[] socURLs = mySocialTagManager.GetAllUrls(aTerm.Term);
    foreach (SocialUrl aUrl in socURLs)
    {
      string urlString = aUrl.Url.ToString();
      if (!SocialUrlStats.ContainsKey(urlString))
      {
        SocialUrlStats.Add(urlString, termName);
      }
      else
      {
        SocialUrlStats[urlString] += ", " + termName;
      }
    }
  }
  catch (UserNotFoundException unfE)
  {
// If the user is not found, handle exception.
  }
}

The Web Part then populates the SocialTagStatsDictionary object by retrieving all social tags that each user in the UserProfileManager has created. It then maps each social tag to its owner.

//Get all the social tags in which the terms have been used.
List<SocialTag> socialTagsList = new List<SocialTag>();
foreach (UserProfile userProf in myUserProfileManager)
{
  try
  {
    SocialTag[] userTags = mySocialTagManager.GetTags(userProf, 0);
    foreach (SocialTag aTag in userTags)
      {
        socialTagsList.Add(aTag);
      }
  }
  catch (UserNotFoundException unfE )
  {
//If the user is not found, handle exception.
  }
}

//For each SocialTag, get the owner and get the number of times that owner has tagged a URL.
foreach (SocialTag aTag in socialTagsList)
{
  if (aTag != null)
  {
    if (!SocialTagStats.ContainsKey(aTag.Owner.DisplayName))
    {
      int tagCount = mySocialTagManager.GetCount(aTag.Owner);
      SocialTagStats.Add(aTag.Owner.DisplayName, tagCount);
    }
  }
}

Finally, the Web Part binds each Dictionary object to a GridView object to display the social data statistics that it has retrieved. The strings used in the user interface are stored in a Resources file.

//Bind each Dictionary to a GridView control. Display localized header strings for each column.
GridView1.DataSource = SocialUrlStats;
GridView1.Columns[0].HeaderText = Properties.Resources.String1;
GridView1.Columns[1].HeaderText = Properties.Resources.String2;
GridView1.DataBind();
GridView2.DataSource = SocialTermStats;
GridView2.Columns[0].HeaderText = Properties.Resources.String3;
GridView2.Columns[1].HeaderText = Properties.Resources.String4;
GridView2.DataBind();
GridView3.DataSource = SocialTagStats;
GridView3.Columns[0].HeaderText = Properties.Resources.String5;
GridView3.Columns[1].HeaderText = Properties.Resources.String2;
GridView3.DataBind();
}

Building and Running the Sample

The following steps demonstrate how you can test this project on your development or test site.

To build the sample

  1. Create a folder named Microsoft.SDK.Server.Samples, and then unzip the SocialDataStatistics.zip file in it.

  2. Start Visual Studio 2010, and then open the SocialDataStatistics.sln file that is in the folder that you created in step 1.

  3. In the Properties window, specify the site URL value of the absolute address of your development or test site (for example, http://mysite/. Ensure that you include the closing forward slash. Also make this URL the value of the socialDataStatsSite string in the VisualWebPart1UserControl.ascx.cs file.

  4. If they are not already present, add references to the following assemblies to the project:

    • Microsoft.SharePoint.dll

    • Microsoft.SharePoint.Taxonomy.dll

    • Microsoft.Office.Server.dll

    • Microsoft.Office.Server.UserProfiles.dll

  5. On the Build menu, select Deploy Solution. After the build is complete, the Web Part is installed on your development or test site.

To run the sample

  • Add the newly installed Web Part to any page on your site. From the Custom category, select the VisualWebPartProject1 Web Part.

    Tip

    If your page generates an error indicating that it cannot find the control for this Web Part, navigate to \Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES, and then rename SocialDataStatisticsWebPart to be VisualWebPartProject1.

See Also

Concepts

Creating and Using Social Data with the Object Model

Other Resources

Code Gallery