Click here to Skip to main content
Click here to Skip to main content
Technical Blog

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

, 30 Apr 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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 imple

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.

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)

Share

About the Author

Kunal Chowdhury (@kunal2383)
Team Leader
India India
Kunal Chowdhury is a Microsoft "Windows Platform Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik MVP, Nokia Developer Champion, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer 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 Store (WinRT) app development. He posts his findings, articles, tutorials in his technical blog and CodeProject.
 
Technical Blog: http://www.kunal-chowdhury.com
Facebook: http://facebook.com/blog.kunal
Twitter : http://twitter.com/kunal2383
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
QuestionNeed little Clarification [modified] PinmemberMember 1114901313-Oct-14 0:33 
QuestionAn alternative: is it possible to post by Native Facebook\Twitter apps installed? Pinmembero.mambelli26-Sep-12 6:47 
AnswerRe: An alternative: is it possible to post by Native Facebook\Twitter apps installed? Pinmvp_ Kunal Chowdhury _26-Sep-12 6:54 
GeneralRe: An alternative: is it possible to post by Native Facebook\Twitter apps installed? Pinmembero.mambelli26-Sep-12 9:10 
AnswerRe: An alternative: is it possible to post by Native Facebook\Twitter apps installed? Pinmvp_ Kunal Chowdhury _26-Sep-12 9: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 Pinmembero.mambelli26-Sep-12 6:40 
GeneralRe: My vote of 4 Pinmvp_ Kunal Chowdhury _26-Sep-12 6:52 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 30 Apr 2012
Article Copyright 2012 by Kunal Chowdhury (@kunal2383)
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid