kunal-chowdhury.com

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.


Kunal Chowdhury
If you have come this far, it means that you liked what you are reading (How to Share Links from WP7 to Social Networking Sites using the ShareLinkTask?).

Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions.

Authored Books: