Click here to Skip to main content
15,881,882 members
Articles / Mobile Apps / Windows Phone 7

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

Rate me:
Please Sign up or sign in to vote.
4.91/5 (4 votes)
30 Apr 2012CPOL3 min read 16.8K   7
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:

C#
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:

C#
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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
QuestionNeed little Clarification Pin
SureshMarepalli12-Oct-14 23:33
SureshMarepalli12-Oct-14 23:33 
QuestionAn alternative: is it possible to post by Native Facebook\Twitter apps installed? Pin
o.mambelli26-Sep-12 5:47
o.mambelli26-Sep-12 5:47 
AnswerRe: An alternative: is it possible to post by Native Facebook\Twitter apps installed? Pin
Kunal Chowdhury «IN»26-Sep-12 5:54
professionalKunal Chowdhury «IN»26-Sep-12 5:54 
GeneralRe: An alternative: is it possible to post by Native Facebook\Twitter apps installed? Pin
o.mambelli26-Sep-12 8:10
o.mambelli26-Sep-12 8:10 
AnswerRe: An alternative: is it possible to post by Native Facebook\Twitter apps installed? Pin
Kunal Chowdhury «IN»26-Sep-12 8:47
professionalKunal Chowdhury «IN»26-Sep-12 8:47 
We can't guess unless the SDK comes out in the market... Frown | :(
Regards - Kunal Chowdhury
Microsoft MVP (Silverlight) | Codeproject MVP & Mentor | Telerik MVP & Insider


Follow me on: My Technical Blog | Silverlight-Zone | Twitter | Facebook | Google+

GeneralMy vote of 4 Pin
o.mambelli26-Sep-12 5:40
o.mambelli26-Sep-12 5:40 
GeneralRe: My vote of 4 Pin
Kunal Chowdhury «IN»26-Sep-12 5:52
professionalKunal Chowdhury «IN»26-Sep-12 5:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.