Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
30 Apr 2012 1  
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 with 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 through 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 an 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 our 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 an 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, its 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 with anybody as we respect your privacy.

Reference: http://www.kunal-chowdhury.com
You may like to follow me on twitter @kunal2383 or may like the Facebook page of my blog http://www.facebook.com/blog.kunal2383.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here