Code Sample: Colleague Approval Social Tagging Application Page

Applies to: SharePoint Server 2010

The Colleague Approval Social Tagging Application page is an application page that lists your colleagues and enables you to note approval of each colleague by selecting a check box. This sample is a Microsoft Visual Studio 2010 SharePoint Application Page project. After you build and deploy this project on your Microsoft SharePoint Server 2010 site, you can use this page to express approval for any user on your list of colleagues. In its current form, the page displays only an asterisk to denote approval. You may replace the asterisk with any symbol or picture that is appropriate to your context.

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.

Using Social Tags to Express Approval of an Item

The application 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. It also retrieves the UserProfile object that represents the current user. The application retrieves all of the current user’s social tags and creates a list of strings that contain the names of the user’s colleagues. Each UserProfile object that represents a colleague of the current user has a PublicUrl property. Any object that can be represented by a URL can be tagged with a social tag, so this application could be revised to enable users to express approval for any set of items that can be represented by URLs, such as items in a SharePoint list or document library.

string mySite = "http://mysite";
protected void Page_Load(object sender, EventArgs e)
{

using ( SPSite site = new SPSite(mySite) )
{


//Create UserProfileManager with desired SPContext.
SPServiceContext serviceContext = SPServiceContext.GetContext(site);
UserProfileManager myUserProfileManager = new UserProfileManager(serviceContext);
UserProfile myUserProfile = myUserProfileManager.GetUserProfile(true);
SocialTagManager mySocialTagManager = new SocialTagManager(serviceContext);
string [] colleagueAccountNames = new string [myUserProfile.Colleagues.GetItems().GetLength(0)];
SocialTag[] mySocialTags = mySocialTagManager.GetTags(myUserProfile);

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 application must perform is to retrieve the TaxonomySession object from the SocialTagManager object. The application then checks to verify whether the taxonomy term that will be used to express approval currently exists in the default keywords term store (the default location for taxonomy terms). This example uses the Thumbs Up term, but this term can be changed to meet your specific requirements. This code can also be placed in the FeatureActivated event receiver, so that it runs only once.

TaxonomySession taxSession = mySocialTagManager.TaxonomySession;
TermStore termStore = taxSession.DefaultKeywordsTermStore;
TermCollection thumbsUp = termStore.KeywordsTermSet.GetTerms("Thumbs Up", true, StringMatchOption.ExactMatch, 1, true);
Term thumbsUpTerm;
if (thumbsUp.Count < 1 || !thumbsUp[0].IsAvailableForTagging)
{
  thumbsUpTerm = termStore.KeywordsTermSet.CreateTerm("Thumbs Up", termStore.DefaultLanguage);
  termStore.CommitAll();
}
else
{
  thumbsUpTerm = thumbsUp[0];
}

The application then stores each colleague in an array of string objects.

int n = 0;
foreach (Colleague colleague in myUserProfile.Colleagues.GetItems())
  { 
    colleagueAccountNames[n] = (string)colleague.Profile[PropertyConstants.AccountName].Value;
    n++;
  }

After performing the work of retrieving the user’s colleagues and setting the taxonomy term that will be used to tag each colleague, the application checks to see whether the page has loaded after a user selected a check box or cleared a check box on the list that displays each of the user’s colleagues. If the page load is the result of a user selecting or clearing a check box on the form, the application verifies which colleague the user selected or cleared. The social tag is added to any colleague whose name has been checked and removed from any colleague whose name has been cleared. The UserProfileManager object retrieves the UserProfile object that represents each colleague, so that it can retrieve, add, or remove social tags from each colleague’s public URL. The application uses the HTML character reference for an asterisk (*) to represent the current user’s expression of approval. You can change or remove this symbol to meet your specific requirements.

if (IsPostBack)
{
  for (int i = 0; i < CheckBoxList1.Items.Count; i++)
  {
    UserProfile userProfile = myUserProfileManager.GetUserProfile(CheckBoxList1.Items[i].Value);
    Uri userUrl = userProfile.PublicUrl;
    bool itemProcessed = false;
    foreach (SocialTag tag in mySocialTags)
    {
      if (tag.Term == thumbsUpTerm && tag.Url == userUrl && CheckBoxList1.Items[i].Selected == false)
      {
        mySocialTagManager.DeleteTag(userUrl, thumbsUpTerm);
        CheckBoxList1.Items[i].Text = CheckBoxList1.Items[i].Value;
        itemProcessed = true;
      }
      else if (tag.Term == thumbsUpTerm && tag.Url == userUrl)
      {
        CheckBoxList1.Items[i].Text = CheckBoxList1.Items[i].Value + " &#42;";
        itemProcessed = true;
      }

    }
    if (CheckBoxList1.Items[i].Selected == true && !itemProcessed)
    {
      SocialTag thumbsUpTag = mySocialTagManager.AddTag(userUrl, thumbsUpTerm);
      CheckBoxList1.Items[i].Text = CheckBoxList1.Items[i].Value + " &#42;";
      itemProcessed = true;
    }
    else if (!itemProcessed)
    {
      CheckBoxList1.Items[i].Text = CheckBoxList1.Items[i].Value;
    }
  }
}

If the page load is the result of a user navigating to the page without selecting or clearing a check box from the list, the application populates the check box list with the names of each of the user’s colleagues and checks whether each colleague has been tagged.

else
{
  CheckBoxList1.DataSource = colleagueAccountNames;
  CheckBoxList1.DataBind();
  for (int i = 0; i < CheckBoxList1.Items.Count; i++)
  {
    UserProfile userProfile = myUserProfileManager.GetUserProfile(CheckBoxList1.Items[i].Value);
    Uri userUrl = userProfile.PublicUrl;
    bool itemProcessed = false;
    foreach (SocialTag tag in mySocialTags)
    {
      if (tag.Term == thumbsUpTerm && tag.Url == userUrl && !itemProcessed)
      {
        CheckBoxList1.Items[i].Selected = true;
        CheckBoxList1.Items[i].Text = CheckBoxList1.Items[i].Value + " &#42;";
        itemProcessed = true;
      }
    }
    if ( !itemProcessed )
    {
      CheckBoxList1.Items[i].Text = CheckBoxList1.Items[i].Value;
    }
  }
}

}
}

Building and Running the Sample

The following steps demonstrate how to 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 RatingColleagues.zip file in it.

  2. Start Visual Studio 2010, and then open the RatingColleagues.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 mySite variable in the ColleagueRating.aspx.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 application page is installed on your development or test site.

Running the Sample

  • After the solution is built and deployed, go to http://mysite/_layouts/ColleagueRating.aspx. Your colleagues’ user account names appear next to check boxes on the page.

See Also

Concepts

Creating and Using Social Data with the Object Model

Other Resources

Code Gallery