ClientContext.Current is null in Silverlight?

Posted on 12/13/2009 @ 4:50 PM in #SharePoint 2010 | 1 comments | 1939 views

Sooner or later, when building Silverlight apps that talk to SharePoint 2010 Client Object Model, you will run into this problem. You’ll try and access ClientContext.Current, and for some mysterious reason, it will be null!

Well, mystery solved. What you need to do is, you need to pass the current SharePoint site URL to the Client Object Model as an InitParam. In short, in your Silverlight Object Tag, add the following:

 

   1: "<param name=\"initParams\" value=\"MS.SP.url=" + SPContext.Current.Site.Url + "\" />"

(The above is shown as code-behind code, because that is where you’d have to add that).

By doing so, the Client Object model can now get access to ClientContext.Current.

Happy SharePointing! w00t!


On 3/8/2010 7:12:07 AM Fabian said ..
I am using HTML Form Web Part but dont can use dinamic url. If i pass a hard code url it work, if i pass SPContext.Current.Site.Url it doesnt work.

Sample:


<param name="InitParams" value="Windows=Main, MS.SP.url=http://mysite, WindowsHeight=220/> - work fine


<param name="InitParams" value="Windows=Main, MS.SP.url=" + SPContext.Current.Site.Url + ", WindowsHeight=220/> - dont work


Please post your comments:


Your feedback will be submitted for moderation, and will appear after it is approved.

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box: