How to Share Links from WP7 to Social Networking Sites using the ShareLinkTask?



Do you want to create an application or game for your Windows Phone 7 that can share links to various social networking sites of users choice? Then this post will help you to integrate the same in your application.

 

Continue reading the complete post to know more about the API, the internal implementation of the WP7 SDK and the way to include it in your application. Don’t forget to share your feedback and if you like the post, please share it to others as this might be helpful for them too.

 

Know About the API

Windows Phone 7 SDK exposes an API named “ShareLinkTask” which allows an application to launch a dialog that enables the user to share a link on the social networks of their choice. ShareLinkTask is a sealed class present in the Microsoft.Phone.Tasks namespace and inherits ShareTaskBase. The class exposes three properties (i.e. LinkUrl, Title, Message) to set the optional values to construct the shared link.

 

Here is the meta data of the ShareLinkTask:

 

 
namespace Microsoft.Phone.Tasks
{
    public sealed class ShareLinkTask : ShareTaskBase
    {
        public Uri LinkUri { get; set; }
        public string Title { get; set; }
        public string Message { get; set; }
    }
}

 

 

Let’s see the internal implementation (decompiled version) of the ShareLinkTask class. If you go thru the below code snippet, you will easily understand how Microsoft implemented the class to share link to social networking site.

 

Here is the decompiled version of the SDK implementation of ShareLinkTask:

 

 
namespace Microsoft.Phone.Tasks
{
  public sealed class ShareLinkTask : ShareTaskBase
  {
    public Uri LinkUri { get; set; }
    public string Title { get; set; }
    public string Message { get; set; }
 
    internal override ParameterPropertyBag BuildParameterPropertyBag()
    {
      ParameterPropertyBag parameterPropertyBag = new ParameterPropertyBag();
      parameterPropertyBag.CreateProperty("MePublishType").Int32Value = 2;
      ParameterProperty property = parameterPropertyBag.CreateProperty("URLLink");
      if (!this.LinkUri.IsAbsoluteUri)
        throw new ArgumentOutOfRangeException("The shared link has to be an absolute Uri.");
      property.StringValue = this.LinkUri.AbsoluteUri;
      parameterPropertyBag.CreateProperty("URLTitle").StringValue = this.Title;
      parameterPropertyBag.CreateProperty("PSM").StringValue = this.Message;
      return parameterPropertyBag;
    }
  }
}

 

I already described about the base class “ShareTaskBase” in the previous blog post. If you didn’t read it, visit this blog post to learn more:

That’s all about the API. Remember that, the LinkUri property always take absolute Uri. If you set relative url, it will throw exception. Now let’s jump into the code to implement it in our application.

 

Implementation Steps

To integrate it in your application you need to create the instance of the class and populate all the optional properties like Title, LinkUri and Message. Once you populated the properties, just give a call to the base method Show() which will popup the application UI to share the already populated link to social networking site.

 

Here is out code snippet:

 
var shareLinkTask = new ShareLinkTask();
shareLinkTask.Title = "WP7 ShareLinkTask Demo: www.kunal-chowdhury.com";
shareLinkTask.LinkUri = new Uri("http://facebook.com/blog.kunal2383", UriKind.Absolute);
shareLinkTask.Message = "If you are a Silverlight or WP7 Geek, " +
                        "don't forget to like my Facebook page: " + 
                        "http://facebook.com/blog.kunal2383" +
                        " for Tech updates.";
shareLinkTask.Show();

 

 

That’s all about the implementation. As Windows Phone 7 emulator doesn’t provide setting up social networking sites on that, it was not possible for me to include screenshots. Try out in your physical device. You will have option to change the content and select the social networking sites of your (users) choice.

 

I hope that this post was very useful for you to understand the SDK API, it’s internal code implementation and the sample code implementation. Please leave your feedback below to leave your comment. Follow my blog to read more articles on Windows Phone 7.

 

Stay tuned to my blog, twitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.

7 comments

  1. Only Windows Live and Facebook seem to show up on my phone. How do i get Twitter and SMS?

    ReplyDelete
  2. Add your Twitter and Linked-In account in your WP7 device and sync them. They will appear in the list. If not, just click the "Post to" box and you will be able to choose any/all of the accounts. Cheers.

    ReplyDelete
  3. great post i used it and works fine :)
    and how can i select facebook by default i dont wan't other apps like twitter,windows live how to implement this

    thanks in advance

    kartheek chowdhury

    ReplyDelete
  4. Unfortunately, Microsoft didn't expose those APIs to the developers. So, you can not handle that explicitly. The only thing that you can do is, just open the page and the user will decide what to chose and what to not.

    ReplyDelete
  5. great post i used it and works fine :)
    I have 2 buttons like Facebook and twitter,if twitter button is clicked then i want show only twitter option even though i added Facebook account in my phone.And same when Facebook will clicked then i want show only Facebook option using share link task.

    ReplyDelete
  6. Kunal,
    Is there anyway where app can directly sent the update to Facebook without any user intervention ?

    Thanks in Advance,
    Santhosh

    ReplyDelete


 
© 2008-2014 Kunal-Chowdhury.com | Designed by Kunal Chowdhury
Back to top