Showing posts with label Client Object Model. Show all posts
Showing posts with label Client Object Model. Show all posts

Wednesday, July 14, 2010

SharePoint 2010 Client Object Model using ADO.NET and REST Services (Retrieve / Update List items)

As I discussed my previous post the Client Object Model allows client application to interact with SharePoint content directly. The ADO.NET Data Services Framework helps to query SharePoint Foundation data via client application.

In this example you will see .NET Managed client by create Windows Form application, and you will see how to retrieve list items and updating the items via Client Object Model.

1. Open Visual Studio 2010 by going to the Start Menu | Programs | Microsoft Visual Studio 2010 | Microsoft Visual Studio 2010.

2. From the menu, select File | New | Project.

3. In the New Project dialog window, choose Visual C# | Windows from the Installed Templates.

4. Select Windows Forms Applications from the Project Templates.

image

5. Go to Visual Studio top menu bar, select Data | Add New Data Source… to add new SharePoint ADO.NET List Data Service.

image

6. In the Data Source Configuration Wizard select Service and click Next > button. When next screen appear Add Service Reference dialog window, type the following Url in the Address text box and click Go:

image

7. Click OK.

8. Click Finish button in the Data Source Configuration Wizard.

9. Go to Solution Explorer, expand References. and Right click on System.Data.Services.Client and select Remove.

 image

10. Right click on References and select Add Reference  and switch to Browse tab and type the following path in the File name textbox and press enter:

C:\Program Files (x86)\ADO.NET Data Services V1.5 CTP2

11. Select Microsoft.Data.Services.Client.dll.

image 

12. Got Visual Studio Data menu and Click Show Data Source System will open the Data Sources window showing the service reference.

image

13. Drag and drop Tasks from the Data Sources window to the Form window. This will insert the Tasks DataGrid into the Form.

14. You can Remove columns by clicking Edit Columns window, Keep only following columns as shown in the screen shot.

image

image

15. Add the following using statement in the code behind:

C#, using GeSHi 1.0.8.8
  1. using SharePointADORESTService.ServiceReference1;
  2. using System.Net;

16. Insert the following code just after the Form1 class declaration:

C#, using GeSHi 1.0.8.8
  1. HomeDataContext context = new HomeDataContext(new Uri("http://demo2010a:1000/_vti_bin/ListData.svc"));

17. Insert the following code in the Form1_Load method

C#, using GeSHi 1.0.8.8
  1. context.Credentials = CredentialCache.DefaultCredentials;
  2. tasksBindingSource.DataSource = context.Tasks;

18. To Update changes from Data Grid to go Tasks DataGrid, select the BindingNavigator right click on the Save button and Select Enable.

image

19. Double click on the Save button in the BindingNavigator to generate the Save method:

C#, using GeSHi 1.0.8.8
  1. context.SaveChanges();

20. Go to TaskBindingSource –> Right Click –> and Go to Properties Select Events. Double click on CurrentItemChanged event. Visual Studio will generate the item changed event for the TasksBindingSource:

C#, using GeSHi 1.0.8.8
  1. context.UpdateObject(tasksBindingSource.Current);

21. Press F5 to start debugging the application.

22. You should see the application window with the list of Tasks items from SharePoint List.

image

23. Click on the first tasks and change the Title value from Faizal from Rafi and click Save and go to SharePoint Tasks list you will able to view the updates. 

image

For Source Code click here

Tuesday, July 13, 2010

SharePoint 2010 Client Object Model (Create/Retrieve/Delete) list

One the most improved feature in SharePoint 2010 is Client Object Model. It enable client application to interact with SharePoint content directly. In Summary “SharePoint 2010 client object model lets you write client-side code to work with all the common objects in SharePoint sites. Through the object model, you can add and remove lists, add, update, and delete list items, change documents in document libraries, create sites, manage permissions of items, and add and remove Web Parts from a page.”

Client Object Module allows to interact following ways.

1. .NET Managed Client

2. Silverlight

3. JavaScript

In this example you will see .NET Managed client by create WPF application, We will see how to retrieve list from SharePoint site, Create list in SharePoint site and Deleting the list from SharePoint site.

Creating SharePoint Client Object Model WPF Application

1. Open Visual Studio 2010 by going to the Start Menu | All Programs | Microsoft Visual Studio 2010 | Microsoft Visual Studio 2010.

2. From the menu, select File | New | Project.

3. In the New Project dialog window, choose Visual C# | Windows from the Installed Templates.

4. Select WPF Application from the Project Items.

image

5. In the solution explorer, right click on References and select Add Reference… and Switch to Browse tab.

image

6. Copy and Paste the following path in the File Name textbox and press enter:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI

7. Select Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll

8. In the Solution Explorer, double click on Window1.xaml to open the XAML view.

Retrieve List Values  

9. Insert the following XAML code inside the Grid element in the XAML view.

HTML, using GeSHi 1.0.8.8




  1. <ListBox Height="225" HorizontalAlignment="Left" Margin="12,61,0,0" Name="listBox1" VerticalAlignment="Top" Width="274" DataContext="{Binding}" />


  2. <Button Content="Retrive Lists" Height="23" HorizontalAlignment="Left" Margin="298,27,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />


  3. <TextBox Height="23" HorizontalAlignment="Left" Margin="64,28,0,0" Name="textBox1" VerticalAlignment="Top" Width="222" />


  4. <Label Content="Site URL" Height="28" HorizontalAlignment="Left" Margin="5,26,0,0" Name="label5" VerticalAlignment="Top" />

Parsed in 0.012 seconds at 46.65 KB/s
image 

10. In the Solution Explorer, right click on Windows1.xaml and select View Code.


image


11. Add the following using statements :

using Microsoft.SharePoint.Client;
using WPFSP = Microsoft.SharePoint.Client;

12. Double click Retriev List button and  Insert the following code block :



C#, using GeSHi 1.0.8.8



  1. private void button1_Click(object sender, RoutedEventArgs e)


  2.         {


  3.             this.Cursor = Cursors.Wait;


  4.             //Set Client Context


  5.             WPFSP.ClientContext context = new WPFSP.ClientContext(textBox1.Text);


  6.             Web site = context.Web;


  7.             context.Load(site, osite => osite.Title);


  8.             context.ExecuteQuery();


  9.             Title = site.Title;


  10.             ListCollection lists = site.Lists;


  11.             IEnumerable<WPFSP.List> listCollection =


  12.                 context.LoadQuery(lists.Include(l => l.Title, l => l.Id));


  13.             context.ExecuteQuery();


  14.             listBox1.ItemsSource = listCollection;


  15.             listBox1.DisplayMemberPath = "Title";


  16.          }







13. Press F5 to start debugging the application.


14. You will see the Application Window with Enter the site Name and Click Retrieve list button System will populate the available SharePoint lists.


image


Creating List via Client Object Module


We will extended the same client Application to create list


15.Insert the following XAML code inside the Grid element in the XAML view.



HTML, using GeSHi 1.0.8.8



  1. <Button Content="Create Lists" Height="23" HorizontalAlignment="Right" Margin="0,295,19,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />


  2. <TextBox Height="23" HorizontalAlignment="Left" Margin="86,296,0,0" Name="textBox2" VerticalAlignment="Top" Width="208" />


  3. <Label Content="List Title" Height="28" HorizontalAlignment="Left" Margin="7,296,0,0" Name="label1" VerticalAlignment="Top" />


  4. <Label Content="Description" Height="28" HorizontalAlignment="Left" Margin="6,323,0,0" Name="label2" VerticalAlignment="Top" />


  5. <TextBox Height="23" HorizontalAlignment="Left" Margin="86,323,0,0" Name="textBox3" VerticalAlignment="Top" Width="208" />


  6. <ComboBox Height="23" HorizontalAlignment="Right" Margin="0,353,98,0" Name="comboBox1" VerticalAlignment="Top" Width="208" ItemsSource="{Binding}">


  7.        <ComboBoxItem Content="Generic List" />


  8.        <ComboBoxItem Content="Announcements" />


  9.        <ComboBoxItem Content="Events" />


  10. </ComboBox>


  11. <Label Content="List Template" Height="28" HorizontalAlignment="Left" Margin="5,348,0,0" Name="label3" VerticalAlignment="Top" />

16. You should see the added items on the designer view

image

17. Double click Create Lists button and  Insert the following code block :



C#, using GeSHi 1.0.8.8



  1. private void button2_Click(object sender, RoutedEventArgs e)


  2.         {


  3.          // Site Collection URL


  4.            ClientContext createContext = new ClientContext(textBox1.Text);


  5.         // Retrieve Sites


  6.            Web mySite = createContext.Web;


  7.         // Rettrive Lists Collections....


  8.             ListCollection lists = mySite.Lists;


  9.         //List Create Information which hold list Title, Description and Template Type


  10.             ListCreationInformation listCreationInfo =


  11.           new ListCreationInformation();


  12.  


  13.             string listType = comboBox1.Text;


  14.  


  15.             switch (listType)


  16.             {


  17.                 case "Generic List":


  18.                     {


  19.                         listCreationInfo.TemplateType = (int)ListTemplateType.GenericList;


  20.                        break;


  21.                     }


  22.                 case "Events":


  23.                     {


  24.                         listCreationInfo.TemplateType = (int)ListTemplateType.Events;


  25.                         break;


  26.                     }


  27.                 case "Announcements":


  28.                     {


  29.                         listCreationInfo.TemplateType = (int)ListTemplateType.Announcements;


  30.                         break;


  31.                     }


  32.             }


  33.  


  34.             // Assign value to list


  35.             listCreationInfo.Title = textBox2.Text;


  36.             listCreationInfo.Description = textBox3.Text;


  37.       


  38.             // Create List     


  39.             mySite.Lists.Add(listCreationInfo);


  40.             createContext.ExecuteQuery();


  41.         }



18. Press F5 to start debugging the application.


19. You will see the Application Window with Enter the site Name, List Name, Description and Select List Template Type and Finally click Create list button System will create SharePoint list.

 image 

20. Go to http://demo2010a:1000/ SharePoint Site View all site content you can able to see the ClientOM Announcements list.image 


Deleting List via Client Object Module


We will extended the same client Application to Delete list


21.Insert the following XAML code inside the Grid element in the XAML view.



HTML, using GeSHi 1.0.8.8



  1. <Button Content="Delete List" Height="23" HorizontalAlignment="Left" Margin="298,399,0,0" Name="button3" VerticalAlignment="Top" Width="75" Click="button3_Click" />


  2. <ComboBox Height="21" HorizontalAlignment="Left" Margin="86,402,0,0" Name="comboBox2" VerticalAlignment="Top" Width="208" DataContext="{Binding ElementName=listBox1}" />


  3. <Label Content="Select List" Height="28" HorizontalAlignment="Left" Margin="6,400,0,0" Name="label4" VerticalAlignment="Top" />
21. You should see the added items on the designer view
image

22. Double click Retriev List button and  Insert the following code block :


            comboBox2.ItemsSource = listCollection;
            comboBox2.DisplayMemberPath = "Title";




23. Double click Delete List button and  Insert the following code block :



C#, using GeSHi 1.0.8.8



  1. private void button3_Click(object sender, RoutedEventArgs e)


  2. {


  3. ClientContext deleteContext = new ClientContext(textBox1.Text);


  4. MessageBox.Show("SharePoint List Deleting " + comboBox2.Text);


  5. deleteContext.Web.Lists.GetByTitle(comboBox2.Text).DeleteObject();


  6. deleteContext.ExecuteQuery();


  7. }

25. You will see the Application Window with Enter the site Name and click Retrieve list button, system will retrieve SharePoint lists and set value to Combo box.

image 


26. Select Value from Combo box and Click Delete List system will delete selected list.


To Download Source code click here




To learn more about Client object model you can refer to  Steve Peschka has written a really good series: