Blog Archives

Google URL Shortener for SharePoint items

You can shorten URLs to make them easier to share using the Google URL shortener. For example, the short URLhttp://goo.gl/l6MS

This is a good way to share and generate short links to share documents.

1) you need to get the API Key for: https://developers.google.com/url-shortener/

2) Install Google APIs Client Library, using NuGet

googleapi

3) Check all References

shorten

4) Use the following method to generate ShortLink

public static string UrlShorter(string largeUrl)
  {
     var finalUrl = string.Empty;
     var key = "API KEY GOOGLE"
     var post = "{\"longUrl\": \"" + largeUrl + "\"}";
     var request = (HttpWebRequest)WebRequest.Create(
             "https://www.googleapis.com/urlshortener/v1/url?key=" + key);
            try
            {
             request.ServicePoint.Expect100Continue = false;
                request.Method = "POST";
                request.ContentLength = post.Length;
                request.ContentType = "application/json";
                request.Headers.Add("Cache-Control", "no-cache");
                using (var requestStream = request.GetRequestStream())
                {
                    byte[] postBuffer = Encoding.ASCII.GetBytes(post);
                    requestStream.Write(postBuffer, 0, postBuffer.Length);
                }
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                  using (var responseStream = response.GetResponseStream())
                  {
                    if (responseStream != null)
                      using (var responseReader = new StreamReader(responseStream))
                      {
                        var json = responseReader.ReadToEnd();
                        var jss = new JavaScriptSerializer();
                     var dict = jss.Deserialize<Dictionary<string, string>>(json);
                        finalUrl = dict["id"]; 
                      }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                System.Diagnostics.Debug.WriteLine(ex.StackTrace);
            }
            return finalUrl;
        }

Thanks

Advertisements

Hide Ribbon and SuitBar of SharePoint 2013 for a specifique Zone – Using JavaScript and C#

One way to hide Ribbon and SuitBar of SharePoint 2013 for a specific zone, like Internet for example – you can use Javascript + Css and run it in a UserControl.

1 – Create a simple UserControl on your solution and add it on your MasterPage.

In ASCX file of your UserControl add the following code:

<script type="text/javascript">
    function hideRibbon(){
        document.getElementById("s4-ribbonrow").style.display = "none !important";
        document.getElementById("suiteBar").style.display = "none !important";
    }
</script>

On code behind of UserControl,  you go execute the javascript function, using the following code:

Example to hide in Internet zone

using Microsoft.SharePoint.Administration;
...

protected void Page_Load(object sender, EventArgs e)
{
 if (SPContext.Current.Site.Zone == SPUrlZone.Internet)
 {
 Page.ClientScript.RegisterStartupScript(this.GetType(),"","hideRibbon();",true);
 }
}