Skip to content

Instantly share code, notes, and snippets.

@DejanMilicic
Created May 15, 2012 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DejanMilicic/2706133 to your computer and use it in GitHub Desktop.
Save DejanMilicic/2706133 to your computer and use it in GitHub Desktop.
N2CMS : Link for sharing content on LinkedIn

LinkedIn share link

N2CMS extension methods for generating LinkedIn share link

public static class N2Extensions
{
public static string FullUrl(this N2.ContentItem item)
{
return HttpContext.Current.Request.Url.GetBaseUri().OriginalString + item.Url;
}
}
public static class SocialExtensions
{
/// <summary>
/// Extension method for producing LinkedIn share link
/// https://developer.linkedin.com/documents/share-linkedin
/// </summary>
/// <param name="item">N2 content item for sharing</param>
/// <param name="title">Optional title. If empty, item Title property will be used. Maximum 200 chars</param>
/// <param name="summary">A brief summary of the article. Longer titles will be truncated gracefully with ellipses.</param>
/// <param name="source">The source of the article. Example: Wired Magazine. Maximum 200 chars</param>
/// <returns>Url which, when returned as link, leads to LinkedIn dialog for sharing</returns>
public static string LinkedinShareUrl(this N2.ContentItem item, string title = "", string summary = "", string source = "")
{
if (String.IsNullOrWhiteSpace(title)) title = item.Title;
string url = String.Format("http://www.linkedin.com/shareArticle?mini=true&url={0}&title={1}",
HttpUtility.UrlEncode(item.FullUrl()), // HttpUtility.UrlEncode for encoding url
Uri.EscapeDataString(title.Substring(0, Math.Min(title.Length, 200)))); // Uri.EscapeDataString for encoding text
if (!String.IsNullOrWhiteSpace(summary)) url = url + "&summary=" + Uri.EscapeDataString(summary);
if (!String.IsNullOrWhiteSpace(source)) url = url + "&source=" + Uri.EscapeDataString(source).Substring(0, Math.Min(source.Length, 200));
return url;
}
}
public static class UriExtensions
{
/// <summary>
/// Extracts the base URL from a <see cref="Uri"/>.
/// </summary>
/// <param name="uri">Specifies the URI from which to extract the base URL.</param>
/// <returns>A base url, for example http://www.foobar.com:8080</returns>
public static Uri GetBaseUri(this Uri uri)
{
return new Uri(string.Format("{0}{1}{2}", uri.Scheme, Uri.SchemeDelimiter, uri.Authority));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment