As we can manage
single SharePoint list items using all the members
and classes exposed by SharePoint 2010 client-side libraries, we
can also manage single list instance as well.
This is for enrich our custom client application with list and
field created at run-time, read operations and updates of their
propreties and behaviours, or with deletions.
Note: first of all, remember to insert into your Visual Studio
2010 project the reference to the libraries
Microsoft.SharePoint.Client.dll and
Microsoft.SharePoint.Client.Runtime.dll
First, we have to chance to retrieve single list instances with
two different methods of the ListCollection class: the GetById and
the GetByTitle method. Respectively thy want as a parameter the
identifier of the list (a GUID) and the display name.
ClientContext context = new ClientContext("http://servername");
using (context)
{
List l = context.Web.Lists.GetByTitle("List name");
}
Instead, if we want to retrieve one or more lists from a
specific SharePoint site, we can use the Lists property of the Web
class in this way:
private static void GetAllLists(ClientContext context)
{
Console.WriteLine("All lists:");
ListCollection allLists = context.Web.Lists;
context.Load(allLists);
context.ExecuteQuery();
foreach (List list in allLists)
{
Console.WriteLine("- {0}", list.Title);
}
}
That property can be also used for simple LINQ query that allow
us to retrieve specific kind of lists. For an example, we can get
back all the lists created from the "Tasks" template, writing down
this simple query:
private static void GetTasksLists(ClientContext context)
{
Console.WriteLine("Tasks lists:");
var query = from list in context.Web.Lists
where list.BaseTemplate == (int)ListTemplateType.Tasks
select list;
var tasksLists = context.LoadQuery(query);
context.ExecuteQuery();
foreach (List list in tasksLists)
{
Console.WriteLine("- {0}", list.Title);
}
}
If we want to create a new istance of a list, using one of the
templates available from the default installation of SharePoint
Foundation 2010, we have to use another important class called
ListCreationInformation. This class deals specify the properties of
the new list to create and pass them to the ClientContext object,
which move to the server.
The new list will be actually created on the SharePoint site only
at the call of the ExecuteQuery() method of the ClientContext
class.
private static List CreateList(ClientContext context)
{
ListCreationInformation info = new ListCreationInformation();
info.Title = "List name";
info.TemplateType = (int)ListTemplateType.Tasks;
List l = context.Web.Lists.Add(info);
context.ExecuteQuery();
return l;
}
Once created, we can now modify one or more of its
properties and send the update to the server.
List l = context.Web.Lists.GetByTitle("List name");
l.OnQuickLaunch = true;
l.Update();
context.ExecuteQuery();
Otherwise, we can modify the fields collection related to
this list.
Usually can be necessary create a new list and configure its fields
basing on the end user choice over out custom client
application.
This operation can be completed using the Fields propery of the
List class and some knowledge about XML syntax used in data
provisioning technique of content types and site
columns.
private static void CreateField(string fieldName, string fieldDisplayName, List l, ClientContext context)
{
Field f = l.Fields.AddFieldAsXml(String.Format("", fieldName, fieldDisplayName), true, AddFieldOptions.AddToAllContentTypes);
context.ExecuteQuery();
}
Even, if we want to delete an istance of a list create on
a SharePoint site, once retrieved the List object with one of the
tecnique viewed before, we simply have to call the
DeleteObject() method.
private static void DeleteList(string listName, ClientContext context)
{
List l = context.Web.Lists.GetByTitle(listName);
l.DeleteObject();
context.ExecuteQuery();
}
Doing all this things we have learned how we can manage
istance of list over SharePoint 2010 using the client-side API into
out custom client applications.