Quantcast
Channel: Frederik Prijck » Silverlight 3
Viewing all articles
Browse latest Browse all 4

Interaction between Sharepoint 2010 and Silverlight 3

$
0
0

To establish a connection between Sharepoint 2010 and Silverlight 3 I have used the new feature in Visual Studio 2010 and Sharepoint 2010.

This function is called “Client Object Model”, it’s a way to make objects of Sharepoint’s Lists etc inside .NET.

To get this to work you should start by making a Silverlight Application inside Visual Studio 2010.

Inside your class where you want to make the connection you need to add 3 things at the top:

private ClientContext clientContext = null;
private Web site = null;
private ListCollection siteLists = null;

The ClientContext object is “the connection” between Sharepoint and .NET.

It’s the medium involved in the communication.

The Web object is an instance of the Sharepoint “Website” and the ListCollection will be a collection of all Lists on the Sharepoint website.

So now we have 3 things, the connection to Sharepoint, an instance of the prefered website and an instance of the Sharepoint Lists.

So now it’s time to initialize the connection.

private void initConnection()
{
clientContext = new ClientContext("http://yoururl");
context.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
context.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo 
{ 
       LoginName="username",
       Password="password",
};
site = clientContext.Web;
clientContext.Load(site);
}

We make an instance of the ClientContext object, this object expect a string parameter, being the website’s URL.
For client side application we also need to authenticate to be able to communicate with the server!
(For webparts this authentication isn’t necessary)

By adding the “site” object to the Load function of the clientContext, all the information of the website will be transferred to the Application (After we have run the executeQueryAsync method)

We can add more than 1 object to the “Load” function, this way we only have to go from Application – Server only once.

So the next thing we do is asking the server to give back all the “Lists” on the Website:

private void loadLists()
{
siteLists = site.Lists;
clientContext.Load(siteLists);
}

Now we can excecute all the Query’s who are in the Load function by using the following command:

try{ clientContext.ExecuteQueryAsync(webSucceededCallback, webFailedCallback);

}catch (Exception ex){}

This “ExcecuteQueryAsync” function expects 2 parameters:

  • The function to excecute when the query’s are successful
  • The function to excecute in the other case

The most important part is, ofcourse, the “webSucceededCallback

Remember, when the query’s has been successful, the “site” object contains an instance of your website, and the “siteLists” a collection of all Lists.

To test wether your connection has been succesful you can bind the siteLists collection to a listbox by using the following command:

Listbox1.ItemsSource = siteLists;
Listbox1.DisplayMemberPath = "Title";

If your Listbox does not contain any objects your query’s didn’t succeed.

Remember to test this application you need to add the Silverlight application as Webpart into Sharepoint 2010.
To do this you just upload your .xap file (which can be found inside this bin\debug directory) to a sharepoint Document Library.

Then add a new Webpart and choose for Silverlight Webpart.
A prompt will ask for the path to the .xap file you have just uploaded!
Fill in the path and test it!

Thanks for reading!

Happy Interacting! ;-)


Viewing all articles
Browse latest Browse all 4

Trending Articles