Saturday 28 October 2017

How to get Site details in SharePoint Online?

These days, Microsoft is obsoleting code based solution from Office 365.  And these leads us to use JavaScript Object Model / Client Side Object Model to get data and add them in to content editor / script editor webpart to display data.

In this blog we will try to get current site title, web tile and any intermediate web title using CSOM. You can go deep down if you have further requirement for these objects.

Here is the basic function to get site details:

Sample code:

var currSiteTitle; // site collection details
var currWebTitle; // get sub site details
var parentWebTitle; // get any intermediate site details

$(document).ready(function() {
 
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() {
            getWebTitle();
     });
});

function getWebTitle() {

        var context = new SP.ClientContext.get_current();
        var site = context.get_site();
        var web = context.get_web();
        context.load(site);
        context.load(web);
        var parentInfo = web.get_parentWeb();  
context.load(parentInfo);
        context.executeQueryAsync(function() {

                console.log(site.get_title());
                console.log(site.get_url());
                console.log(web.get_title());
                console.log(web.get_url());
                console.log("Intermediate site value: " + parentInfo.get_title());

                currSiteTitle = site.get_title();
                currWebTitle = web.get_title();
                parentWebTitle = parentInfo.get_title();
            },
            function(sender, args) {
                console.log(args.get_message());
            }
        );
     }


Original Source:
  1. https://msdn.microsoft.com/en-us/library/office/jj246780.aspx
  2. https://msdn.microsoft.com/en-us/library/office/jj246877.aspx
  3. https://sharepoint.stackexchange.com/questions/175217/get-parent-web-of-a-subsite-using-jsom-in-sharepoint-2013

Tuesday 3 October 2017

QueryString-error-The name request does not exist in the current context

After a long time, I was working on .net project. Basically I forget all basic things and on of them was how to use Query String. So here is blog for those who face issue same as me when user querystring and get error :

The name request does not exist in the current context

Below was code I was using:

if (!String.IsNullOrWhiteSpace(Convert.ToString(Request.QueryString["Source"])))
                {
                    Context.Response.Redirect(Convert.ToString(Request.QueryString["Source"]));
                }


Solution:

Solution is very easy. First, you need to add below reference in your code:

using System.Web;

This will allow you to use "HttpContext.Current" in your code. So your final solution should look like below:

if (HttpContext.Current != null && !String.IsNullOrWhiteSpace(Convert.ToString(HttpContext.Current.Request.QueryString["Source"])))
                {
                    Context.Response.Redirect(Convert.ToString(HttpContext.Current.Request.QueryString["Source"]));
                }