Using Social Data Web Service in Sharepoint 2010

Custom Social Data WebPart -
The Social Data Web Service provides rich access to a number of key social APIs in SharePoint. In the example below i will show you how to use the service and create a custom social data webpart. To use the service in an application, follow these steps:

1. Open Visual Studio 2010 and click File ➪ New ➪ Project.
2. Select Empty SharePoint Project. Provide a name for your application (for example,
SocialRatingWebPart), and then click OK. When prompted, select "Deploy as farm solution"
and click Finish.
3. When Visual Studio creates the project, right-click the project and select Add ➪ New Item. In the SharePoint 2010 project node, select Visual Web Part. Provide a name for the Web part (for example,SocialRatingData) and click Add.
4. Right-click the .ascx file and select View Designer. Click the Source tab, and then add the following code below to the ascx code behind:

<asp:UpdatePanel ID="RatingUpdatePanel" runat="server">
<ContentTemplate>
<asp:Label ID="lblSocialRatingTitle" runat="server" Font-Bold=”True” ForeColor="#000066" Text="Rating Data"></asp:Label>
<table>
<tr>
<td><asp:Label ID="lblRatingDataList" runat="server" ForeColor="#000066" Text=”Rating Data"></asp:Label></td>
<td><asp:ListBox ID="lstbxRatingData" runat="server" Width="172px"></asp:ListBox>
</td></tr><tr><td>
<asp:Label ID="lblRating" ForeColor="#000066" runat="server" Text="Avg. Rating:"></asp:Label>
</td><td>
<asp:Label ID="lblData" ForeColor="#000066" runat="server" Text=”Data”></asp:Label>
</td></tr></table><table><tr>
<td><asp:Button ID="btnRefresh" runat="server" Text="Refresh"
ToolTip="Click to refresh." onclick="btnRefresh_Click" /></td>
<td></td></tr></table>
</ContentTemplate>
</asp:UpdatePanel>

7. Click the Design tab to switch into the Designer view and then double-click the btnRefresh button.

8. When the code behind opens, add the following code to the code behind:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SocialRatingWebPart.SocialWS;

namespace SocialRatingWebPart.SocialRatingData
{
public partial class SocialRatingDataUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnRefresh_Click(object sender, EventArgs e)
{

// Add the URLs for your wiki sites here(as an example)
string ratingonwiki1URL = “http://SpSite/Wiki1/Home.aspx”;

string ratingonwiki2URL = “http://SpSite/Wiki2/Pages/Home.aspx”;

string ratingonwiki3URL = “http://SpSite/Wiki3/Pages/Home.aspx”;

SocialDataService mySocialDataService = new SocialDataService();
mySocialDataService.Credentials = System.Net.CredentialCache.DefaultCredentials;
mySocialDataService.Url = “http://intranet.contoso.com/_vti_bin/socialdataservice.asmx”;

SocialRatingDetail ratingonwiki1 = mySocialDataService.GetRatingOnUrl(ratingonwiki1URL);

SocialRatingDetail ratingonwiki2 = mySocialDataService.GetRatingOnUrl(ratingonwiki2URL);

SocialRatingDetail ratingonwiki3= mySocialDataService.GetRatingOnUrl(ratingonwiki3URL);

addRatingsToWebPart(ratingonwiki1.Rating, ratingonwiki2.Rating, ratingonwiki3.Rating);
mySocialDataService.Dispose();
}

private void addRatingsToWebPart(int Rate1, int Rate2, int Rate3)
{
int avgRating = 0;
string RtWiki1 = “Rating for Wiki1: “ + Rate1.ToString();
string RtWiki2 = “Rating for Wiki2: “ + Rate2.ToString();
string RtWiki3 = “Rating for Wiki3: “ + Rate3.ToString();
avgRating = (RtWiki1 + RtWiki2 + RtWiki3) / 3;
string avgRatingForWikis = “Average Rating: “ + avgRating.ToString();
lstbxRatingData.Items.Add(RtWiki1);
lstbxRatingData.Items.Add(RtWiki2);
lstbxRatingData.Items.Add(RtWiki3);
lstbxRatingData.Items.Add(avgRatingForWikis);
lblData.Text = avgRating.ToString();
}
}}
9. Amend the .webpart file to have a more intuitive title and description, such as shown here:
<properties>
<property name=”Title” type=”string”>Wiki Rating Web Part</property>
<property name=”Description” type=”string”>Web Part that displays
wiki rating data.</property>
</properties>

10. Press F6 to build the project. When the project successfully builds, click Build ➪ Deploy Solution to deploy the new Visual Web part to SharePoint.
11. Navigate to your SharePoint site. Create a new Web part page and then click "Add a new web part."
12. In the Custom category, select your newly created Web part and click Add. When the Web part is added to the page, click the Refresh button. You should see the social data service load information into the Web part.

2 comments:

  1. But how do you create the web part connection? I tried and Visual Studio dosen't have "Add Web Service Referenace" in the menu. And when I add a regular Service reference I get an endpoint error

    ReplyDelete
  2. nevermind I figured it out:
    * Right-click the project and choose 'add service reference'
    * click the "advanced button"
    * click the 'add web reference button'
    * enter your URL and complete the reference name.
    * Click "Add Reference"

    ReplyDelete

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at sharepointprogrammingblogger@gmail.com .

Copyright (c) 2010 @ myshaepointwork.blogspot.com. All rights are reserved.Do Not Copy.

@ Learning SharePoint.com