SharePoint - Getting site url dynamically in SharePoint 2010 Client Object Model

Asked By Madhu Shekar on 03-May-12 07:51 AM
Hi Everyone,

I want to get site url dynamically in SharePoint 2010 Client Object Model using .net managed code like we get in server side object model i.e, string url = spcontext.current.site.url;

how can I get in Client Object Model

thanks in advance.....
shekar
Jitendra Faye replied to Madhu Shekar on 04-May-12 01:25 AM
I think tyou want to get Client context.

Here you need to use using Microsoft.SharePoint.Client namespace.

Now use this code-

ClientContext clientContext = new ClientContext("URL"); 
Web site = clientContext.Web;
Madhu Shekar replied to Jitendra Faye on 04-May-12 01:31 AM
Hi Vickey,

Thank you for your reply.

What ever url we are passing in clientcontext by hardcode, I want to get that url dynamically i.e like spcontext .current.site.url as we us in server side object model.

thanks,
Shekar
Jitendra Faye replied to Madhu Shekar on 04-May-12 03:36 AM
Ok, Then you can use -

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.clientruntimecontext.url.aspx -- Gets the URL associated with the runtime context. (Inherited from http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.clientruntimecontext.aspx.)

Refer this link-=
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.clientcontext_members.aspx
Jacob Söderblom replied to Madhu Shekar on 07-May-12 08:53 AM
You can try to use javascript! JavaScript:document.location.href; But then you need to trim the url a bit i think :)

Have a nice day!