RSS

Tag Archives: visual web part

SharePoint 2010 Twitter Web Part


It’s been a while time since my last post here.Today I’m going to talk about Building Twitter web part in SharePoint 2010 as seen below.

image

Note:Before you start you can download code from here ( Please do not forget to rate it)

SharePoint 2010 Twitter Web Part

SharePoint 2010 Twitter Web Part Features

  • Allow you to display Profile or search with include replies and include retweets when you select Profile mode
  • you can specify numbers of tweets or status(1,2,..etc)
  • The Status will be updated during specific time(10,15,30,45,16) asynchronously (no need to refresh page).
  • It’s Free Open-mouthed smile

Twitter Web Parts important things to know

1- Define and pass custom parameters to Visual web part.

if you look to the following image you will see that I’m using custom properties to initialize web part,this is easy in normal web part but not in visual web part

image

To get rid this issue you need to follow this steps

  • Open your web part class that inherits from ASP,Net WebPart class
  • Add custom properties let say Profile
public class TwitterWepPart : WebPart
{
public enum TwitterMode
{
Profile,
Search
}
[DefaultValue(TwitterMode.Profile)]
[Description("Select a category from the dropdown list.")]
[WebDisplayName("Twitter Mode")]
[WebBrowsable(true)]
[Personalizable(PersonalizationScope.User)]
[Category("Twitter Settings")]
public TwitterMode Mode
{
get;
set;
}

……

}

as you can see I’m using Enum to display the value in DropDown List

  • After Defining the whole custom properties you need to define a property of TwitterWebPart in user control
public partial class TwitterWepPartUserControl : UserControl
{

public TwitterWepPart PropertiesWebPart;

…….

}
  • The final step is to pass the custom properties within CreateChildControls method
public class TwitterWepPart : WebPart
{

…..

// Visual Studio might automatically update this path when you change the Visual Web Part project item.
private const string _ascxPath = @"~/_CONTROLTEMPLATES/SPTwitter/TwitterWebPart/TwitterWebPartUserControl.ascx";

protected override void CreateChildControls()
{
TwitterWebPartUserControl control = Page.LoadControl(_ascxPath) as TwitterWebPartUserControl;
control.PropertiesWebPart = this;
Controls.Add(control);
}

}

….

}

As you can see  Page.LoadControl is used to load the control of type Control but in our case we want LoadControl to return TwitterWebUserControl instead of Control,so we can read the values of Custom Properties of this web part from usercontrol.

var Tweets = GetProfile(PropertiesWebPart.Name, PropertiesWebPart.Count, PropertiesWebPart.Retweet, PropertiesWebPart.Replies);

We are able now read value from Custom Properties like count,including Retweets ..etc,

2- Using Twitter API to display the results

For user Mode you need to pass the following parameters to twitter

  • User name with or without @
  • Number of status to display
  • Include Replies (optional)
  • Include Retweets (Optional)

you will then Create Get request as following

http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=amdnaji&count=5&include_rts=1&exclude_replies=1

For Search Mode you only need the following Parameters

  • Search Name
  • Number of status to display

The request will be like this

http://search.twitter.com/search.atom?q=ajax&rpp=5

Start working on web part

1- Open the project on Visual studio 2010 –>Deploy

Note:You need to change Site Url before deploy web part.

2- Open your site then add web part to the page.

3- Click on Edit Web Part and go to Twitter Settings Section

image

Let us select Profile as Twitter Mode and in the name let us type Dr oz account @DrOz and Count will be 3.

Click ok to close web part panel and save the page then you will find the following results

image

Now Let us change the mode to Search and select SharePoint 2010 as a name of this Search.

image

References

I hope you like this web part and really appreciate your comments and feedback

Regards.

Advertisements
 
13 Comments

Posted by on January 17, 2012 in SharePoint

 

Tags: , , , ,