Accessing SharePoint 2010 Data from Silverlight App - Doesn't Work
-
יום ראשון 15 אפריל 2012 21:19
Thanks for clicking.
I've been searching all over every forum I could find to help resolve this issue but haven't gotten anywhere. I'm trying to access SharePoint data through a silverlight app. Both are in the same intranet. I'm doing this in vb.net, but will demonstrate the issue in C#, since all of the relevant information seems to be posted in C# anyways.
Ultimately, I want to be able to upload a file to a specific SharePoint library using a silverlight app. That app is being hosted within SharePoint. However, just to get started, I wanted to see if I could access ANY SharePoint information from the silverlight app, so I built a test based on an example I found. The example is a Windows Console Application (Visual Studio '10 -> New Project -> Windows -> Console Application, using .net 3.5). I add a reference to Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime, then add the following code to Program.cs:
using System; using Microsoft.SharePoint.Client; class Program { static void Main() { ClientContext clientContext = new ClientContext("http://SharePoint"); Web site = clientContext.Web; clientContext.Load(site); clientContext.ExecuteQuery(); Console.WriteLine("Title: {0}", site.Title); } }This code works fine.
However, when I try to build the same concepts into a Silverlight application by starting a new project Silverlight -> Silverlight Application, I have to add the Microsoft.SharePoint.Client.Silverlight and Microsoft.SharePoint.Client.Silverlight.Runtime references, which could be the issue, I'm not sure. I use the following code:
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.SharePoint.Client; namespace SharepointTestC { public partial class MainPage : UserControl { ClientContext clientContext = new ClientContext("http://SharePoint"); Web site; public MainPage() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { site = clientContext.Web; clientContext.Load(site); clientContext.ExecuteQueryAsync(webSucceededCallback, webFailed); } void webSucceededCallback(object sender, ClientRequestSucceededEventArgs args) { Dispatcher.BeginInvoke(() => { label1.Content = site.Title; }); } void webFailed(object sender, ClientRequestFailedEventArgs args) { Dispatcher.BeginInvoke(() => { label1.Content = args.ErrorCode; }); } } }And it fails every time, with ErrorCode=-1. I can't see what's wrong with the setup. Can someone please explain why the Silverlight code won't execute properly? I'm at a total loss.
Many thanks,
Kevin
Kevin McGrath
כל התגובות
-
יום שני 16 אפריל 2012 14:01
Try this approach: http://praveenbattula.blogspot.com/2010/03/sharepoint-2010-silverlight-client.html
You should add the Microsoft.SharePoint.Client.Silverlight and Microsoft.SharePoint.Client.Silverlight.Runtime
Rahul Gupta, MCITP, MCPD - SharePoint 2010
- הוצע כתשובה על-ידי Arun Kumar Arora יום שני 16 אפריל 2012 14:02
-
יום שני 16 אפריל 2012 14:19
Hi thanks for the reply, and I'm going to give that a try right now. However, the method in that link still requires the file be deployed inside SharePoint. Is there no way to host a silverlight application elsewhere within an intranet, and allow it to access SharePoint information?
The one thing I can think of that would make this compicated is that a silverlight application running outside of SharePoint might run with different permissions (i.e. not the user logged into the workstation).
Appreciate your thoughts on this.
Thanks,
KevinKevin McGrath
-
יום שני 16 אפריל 2012 14:31
Silverlight Applications can access data from Sharepoint even from outside the SP Environment. You can specify the Authentication by these two properties of ClientContext - AuthenticationMode ; if utilizing (forms) FormsAuthenticationLoginInfo.
Rahul Gupta, MCITP, MCPD - SharePoint 2010
-
יום שני 16 אפריל 2012 14:32Also, have you tried to debug the application and retrieved the exception and inner exception for the issue.
Rahul Gupta, MCITP, MCPD - SharePoint 2010
-
יום שני 16 אפריל 2012 14:42
Hi yes, when debugging the inner exception for "args" in the webFailed is:
{System.Security.SecurityException: Security error.
at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClassa.<EndGetResponse>b__9(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__1(Object sendState)}
Kevin McGrath
-
יום שני 16 אפריל 2012 14:49What is the authentication type configured on your target web application?
Rahul Gupta, MCITP, MCPD - SharePoint 2010
-
יום שני 16 אפריל 2012 15:10Authentication type is Windows authentication.
Kevin McGrath
Actually, authentication for the C# program above is something I'm not 100% sure about - it's whatever the default authentication type is for a silverlight 4 application.- נערך על-ידי KevinM09 יום שני 16 אפריל 2012 15:21 Correction
-
יום שני 16 אפריל 2012 15:39
Try this:
ClientContext clientContext = new ClientContext("");
clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
clientContext.Credentials = new NetworkCredential("username", "password", "domain");If you are not using a service account then pass the default credentials from the Current Application Context to the Credentials object.
Rahul Gupta, MCITP, MCPD - SharePoint 2010
-
יום שני 16 אפריל 2012 15:48
I was looking for something like that! Here's the problem now, though:
"'Microsoft.SharePoint.Client.ClientContext' does not cnotain a definition for 'AuthenticationMode'".
Is there something wrong with the Microsoft.SharePoint.Client.Silverlight and Microsoft.SharePoint.Client.Silverlight.Runtime references I'm using?
Looking into that now and will report back shortly.
Kevin McGrath
-
יום שני 16 אפריל 2012 15:49Just for testing purposes can you skip the silverlight namespaces and utilize the Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime with credentials.
Rahul Gupta, MCITP, MCPD - SharePoint 2010
-
יום שני 16 אפריל 2012 16:09
I'm able to run the console application just fine using the clientContext.AuthenticationMode cod you provided. However, I still have to enter the context when I run it. That is to say: "ClientContext clientContext = new ClientContext("");" won't compile - I need to give the url: ClientContext clientContext = new ClientContext("http:/ /SharePoint");
However, so long as I do that, the console application works fine with just:
ClientContext clientContext = new ClientContext("http:/ /SharePoint"); clientContext.AuthenticationMode = ClientAuthenticationMode.Default;Or with with:
ClientContext clientContext = new ClientContext("http:/ /SharePoint"); clientContext.AuthenticationMode = ClientAuthenticationMode.Default; clientContext.Credentials = new NetworkCredential("username", "password", "domain");I cannot use the Microsoft.Sharepoint.Client and Microsoft.SharePoint.Client.Runtime references in the silverlight project, though - I get an error:
"You can't add ar eference to Microsoft.SharePoint.Client.Runtime.dll as it was not built against the Silverlight runtime. Silverlight projects will only work with Silverlight assemblies".
Kevin McGrath
-
יום שני 16 אפריל 2012 16:30
Hi
Might be caused by missing client plicy access file.
Have a look at this site: http://snahta.blogspot.com/2010/05/silverlight-security-error-connecting.html
Some more info: http://msdn.microsoft.com/en-us/library/cc197955(v=vs.95).aspx
Regards Bjoern
- סומן כתשובה על-ידי KevinM09 יום שני 16 אפריל 2012 16:52
-
יום שני 16 אפריל 2012 16:54
That was it!
Thank you so much to both of you - seems like such a simple fix, but I didn't know to look for a ClientAccessPolicy.
Thank you both VERY much for your consideration, and your time. That makes my life so much easier, and creates all kinds of potential for our organization in terms of rich UI interfaces in SharePoint. Very excited about this...
Cheers,
Kevin
Kevin McGrath
-
יום ראשון 05 אוגוסט 2012 19:11for Silverlight client OM, and you want to allow anonymous access to sharepoint data, you need to run the following PoweShell commands
$webapp = Get-SPWebApplication “http://URL”
$webapp.ClientCallableSettings.AnonymousRestrictedTypes.Remove([microsoft.sharepoint.splist], “GetItems”)
$webapp.Update()
hope this help :)
Mahmoud
Exceed IT Services -UAE