|
Now it is time to create, update and delete Views of a list. I suggest that you read all previous posts before continuing on.
|
| So once again; to create a new View we POST to the Views collection of a List. We will use our good old List that we created before. Mine is located at this URI; http://jndtvbx64/mashpoint/1/subweb/MyDocLib. |
If you compare this URI to the URI we used in the previous post you notice that they are not the same. This one is much shorter than the one we used before. There are actually different URIs to the same resource.
|
|
| Click on the Views link and you will see the list of available Views for this list. |
|
| We will create a new View called "Created By Me" and it should display the following fields; DocIcon, LinkFileName, Author and Editor. Below is the code snippet to create this View. |
[Test]
public void CreateListView()
{
string uri = L("http://server/mashpoint/1/subweb/MyDocLib/Views");
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials;
NameValueCollection values = new NameValueCollection();
// required
values["Name"] = "Created By Me";
values.Add("ViewField", "DocIcon");
values.Add("ViewField", "LinkFilename");
values.Add("ViewField", "Author");
values.Add("ViewField", "Editor");
// optional
values["Query"] = @"<Where>" +
"<Eq>" +
"<FieldRef Name='Author' />" +
"<Value Type='Integer'>" +
"<UserID Type='Integer' />" +
"</Value>" +
"</Eq>" +
"</Where>";
values["RowLimit"] = "50";
values["Paged"] = true.ToString();
values["DefaultView"] = false.ToString();
values["PersonalView"] = true.ToString();
WebClientHelper.UploadValues(uri, "POST", wc, values);
string newuri = wc.ResponseHeaders.Get("Location");
Console.WriteLine(newuri);
}
|
You might ask why we are using WebClientHelper.UploadValues instead of wc.UploadValues. The reason for this is that the WebClient class has a bug. It doesn't send multi valued fields properly. So we have created this helper class. This is the second shortcoming of the WebClient class we have run into. If someone knows about a better helper class to use we would love to hear from you.You can download WebClientHelper.cs here.
|
|
| To update the View we send a PUT request to the URI of the newly created View. Below is the code to change RowLimit from 50 to 100. |
[Test]
public void UpdateListView()
{
string uri = L("http://server/mashpoint/1/subweb/MyDocLib" +
"/Views/e0f6267a-e7f3-4184-912d-96e720c9c0b5");
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials;
NameValueCollection values = new NameValueCollection();
values["RowLimit"] = "100";
wc.UploadValues(uri, "PUT", values);
}
|
| And to delete a View we send a DELETE request to the URI of the View. |
[Test]
public void DeleteListView()
{
string uri = L("http://server/mashpoint/1/subweb/MyDocLib" +
"/Views/e0f6267a-e7f3-4184-912d-96e720c9c0b5");
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials;
wc.UploadString(uri, "DELETE", string.Empty);
}
|
| In the next post we will see how to create ContentTypes. But if you have read all these posts you should be able to figure out how to do this on your own ;) |