Thursday, December 23, 2010

SharePoint 2010 REST services and Silverlight Impersonation

New with SP 2010 comes a Client Object Model, but what makes it tick?  There is a new service layer exposed WCF services.  There are some limitations to the Client Object Model, one of these is the inability to run with elevated privileges.  To avoid this, we are going to connect directly to the services under the Client Object Model, REST services in this case. 

In this example, I will request tasks from a SharePoint Task list in Silverlight.  Nothing fancy, just to show the technique, and also how to connect with impersonated credentials.  This post came from an answer I posted on the MSDN forums.

Silverlight and security – what you should know

Starting with a simple Silverlight Application – The Main View XAML

<StackPanel Orientation="Vertical" x:Name="LayoutRoot">
<Button Content="Click Me" Click="Button_Click"></Button>
<TextBox x:Name="textResults"></TextBox>
</StackPanel>




And the code behind:

private void Button_Click(object sender, RoutedEventArgs e)
{
WebRequest.RegisterPrefix("http://", System.Net.Browser.WebRequestCreator.ClientHttp);
var client = new WebClient();
client.Credentials = new NetworkCredential("user", "password", "domain");
client.UseDefaultCredentials = false;
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(new Uri("http://sp2010/_vti_bin/ListData.svc/Tasks"));
}

private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
Dispatcher.BeginInvoke(() => textResults.Text = e.Result);
}




Now, if you look @ the Uri specified,


“http://sp2010/_vti_bin/ListData.svc/Tasks” – You should be able to browse directly there and get the subscribe/ATOM page.


When the above Silverlight file is run as a SharePoint web part, get the following back.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://sp2010/_vti_bin/listdata.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">Tasks</title>
<id>http://sp2010/_vti_bin/ListData.svc/Tasks/</id>
<updated>2010-12-23T04:57:17Z</updated>
<link rel="self" title="Tasks" href="Tasks" />
<entry m:etag="W/&quot;1&quot;">
<id>http://sp2010/_vti_bin/listdata.svc/Tasks(2)</id>
<title type="text">Test 2</title>
<updated>2010-10-30T16:54:55-04:00</updated>
<author>
<name />
</author>
<link rel="edit" title="TasksItem" href="Tasks(2)" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CreatedBy" type="application/atom+xml;type=entry" title="CreatedBy" href="Tasks(2)/CreatedBy" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ModifiedBy" type="application/atom+xml;type=entry" title="ModifiedBy" href="Tasks(2)/ModifiedBy" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Attachments" type="application/atom+xml;type=feed" title="Attachments" href="Tasks(2)/Attachments" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Predecessors" type="application/atom+xml;type=feed" title="Predecessors" href="Tasks(2)/Predecessors" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Priority" type="application/atom+xml;type=entry" title="Priority" href="Tasks(2)/Priority" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Status" type="application/atom+xml;type=entry" title="Status" href="Tasks(2)/Status" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/AssignedTo" type="application/atom+xml;type=entry" title="AssignedTo" href="Tasks(2)/AssignedTo" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/TaskGroup" type="application/atom+xml;type=entry" title="TaskGroup" href="Tasks(2)/TaskGroup" />
<category term="Microsoft.SharePoint.DataService.TasksItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Id m:type="Edm.Int32">2</d:Id>
<d:ContentTypeID>0x0108008CF743378A195C4D9AC3C5A5D0168D42</d:ContentTypeID>
<d:ContentType>Task</d:ContentType>
<d:Title>Test 2</d:Title>
<d:Modified m:type="Edm.DateTime">2010-10-30T16:54:55</d:Modified>
<d:Created m:type="Edm.DateTime">2010-10-30T16:54:55</d:Created>
<d:CreatedById m:type="Edm.Int32">6</d:CreatedById>
<d:ModifiedById m:type="Edm.Int32">6</d:ModifiedById>
<d:Owshiddenversion m:type="Edm.Int32">1</d:Owshiddenversion>
<d:Version>1.0</d:Version>
<d:Path>/Lists/Tasks</d:Path>
<d:PriorityValue>(2) Normal</d:PriorityValue>
<d:StatusValue>Not Started</d:StatusValue>
<d:Complete m:type="Edm.Double" m:null="true" />
<d:AssignedToId m:type="Edm.Int32">6</d:AssignedToId>
<d:TaskGroupId m:type="Edm.Int32" m:null="true" />
<d:Description>&lt;div&gt;Testing&lt;/div&gt;</d:Description>
<d:StartDate m:type="Edm.DateTime">2010-10-30T00:00:00</d:StartDate>
<d:DueDate m:type="Edm.DateTime" m:null="true" />
</m:properties>
</content>
</entry>
</feed>
 

0 comments:

Post a Comment