Geeks With Blogs
mike's Blog (mr. NICE guy) "Talent, Ambition and Work Ethics ~ my way of being a SharePoint Mutt"

In this post I’m going to demonstrate how to use Collaborative Application Markup Language(CAML) to retrieve and display SharePoint Data in Windows Forms Application.

1. Open Visual Studio 2010 and Create a New Project, on the project template type select Windows Forms Application.

2. After the project is created right click on the References node and select Add Reference, with the Add Reference dialog open click on Browse tab then navigate to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI the select the two DLL namely (Microsoft.Sharepoint.Client.dll and Microsoft.Sharepoint.Client.Runtime.dll), then click OK to close the dialog.

3. Your project should now look like below:

solution

4. Next, open the form in designer then drag a button on the surface of the form. Your form should look like the one below:

caml

5. With the form and the button in place double click the button to open the code window and type the following code on the buttons onclick event. On the declaration section of the class declare a string variable to the SharePoint site URL that you want to get data. Also add a using statement to reference the Sharepoint Client DLLs. (using SP = Microsoft.Sharepoint.Client)

Note: In this example you need to have a SharePoint List on the SharePoint site with a name SampleList with some data contained in it.

string _context = “theurlofthesharepointsite”;

private void cmdload_Click(object sender, EventArgs e)
{
    using (SP.ClientContext clientcontext = new SP.ClientContext(_context))
    {
        SP.List list = clientcontext.Web.Lists.GetByTitle("SampleList");
        SP.ListItemCollectionPosition _itemposition = null;
        while (true)
        {
            SP.CamlQuery _query = new SP.CamlQuery();
            _query.ListItemCollectionPosition = _itemposition;
            _query.ViewXml = @"
                        <View>
                            <Query>
                                <Where>
                                    <IsNotNull>
                                        <FieldRef Name = 'Title'/>
                                    </IsNotNull>
                                </Where>
                            </Query>
                            <RowLimit>10</RowLimit>
                        </View>";
            SP.ListItemCollection _listitems = list.GetItems(_query);
            clientcontext.Load(_listitems);
            clientcontext.ExecuteQuery();

            _itemposition = _listitems.ListItemCollectionPosition;

            foreach (SP.ListItem listItem in _listitems.ToList())
            {
                MessageBox.Show("Title: " + listItem["Title"]);
            }

            if (_itemposition == null)
            {
                break;
            }
            MessageBox.Show("Position: " + _itemposition.PagingInfo);
        }
    }
}

That’s it, hit F5 to run the application then click the Load button, a Message Box will appear containing each Title of the SharePoint List Items contained in the SampleList List.

Hope this helps…! Smile

Posted on Tuesday, April 17, 2012 8:49 PM Client Object Model | Back to top


Comments on this post: How to use CAML Query to retrieve and display SharePoint Data in Windows Forms Application

# re: How to use CAML Query to retrieve and display SharePoint Data in Windows Forms Application
Requesting Gravatar...
A feel to be proud after read your article. Actually its a need of human and Developing is must for us successful in life.
Left by kent campervan hire on Apr 17, 2012 9:32 PM

# How to reduce your spine pain by the laser spine surgery
Requesting Gravatar...
I used to be more than happy to find this web-site. I needed to thanks for your time for this glorious learn!! I positively enjoying each little bit of it and I have you bookmarked to check out new stuff you blog post.
Left by Laser Spine Surgery on Apr 24, 2012 5:53 PM

Your comment:
 (will show your gravatar)


Copyright © Michael M. Bangoy | Powered by: GeeksWithBlogs.net