Clone a view of a template list to your new list
using the SharePoint Webservice Views.asmx .
private void CloneViewXML(string mytemplateUrl ,string myurl, string mylist, string myview )
{
SetStatus("Initialize webservice...");
SetStatus("Calling webservice...");
Boolean retcode = true;
WS.Views views = new WS.Views();
if (chk_DefaultLoggin.Checked)
views.Credentials = System.Net.CredentialCache.DefaultCredentials;
else
{
views.PreAuthenticate = true;
views.Credentials = new System.Net.NetworkCredential(txt_UserID.Text, txt_PassWord.Text, txt_domain.Text);
}
views.Url = mytemplateUrl + @"/_vti_bin/Views.asmx";
try
{
this.SetStatus("getting viewxml for view " + myview + " on list " + mylist );
XmlNode myListNode = views.GetViewCollection(mylist);
XmlNodeList lnodes = myListNode.SelectNodes("*");
String viewGuid = "";
XmlNode vnQuery = null;
XmlNode vnFields = null;
XmlNode vnRowLimit = null;
foreach (XmlNode lnode in lnodes)
{
// ID of the View
if (lnode.Attributes["DisplayName"].Value == myview)
viewGuid = lnode.Attributes["Name"].Value;
}
/// if no guid found... exit
if (viewGuid != "")
{
XmlNode viewxml = views.GetView(mylist, viewGuid); // internal listname en view guid
String sViewType = viewxml.Attributes["Type"].Value;
XmlNodeList nodes = viewxml.SelectNodes("*");
foreach (XmlNode node in nodes)
{
if (node.Name == "Query")
vnQuery = node;
if (node.Name == "ViewFields")
vnFields = node;
if (node.Name == "RowLimit")
vnRowLimit = node;
}
if (vnQuery != null && vnFields != null && vnRowLimit != null)
{
if (TMSI_Trace_On.Checked)
this.traceThis("Creating view...");
// call function to create the new view...
retcode = CreateView(myurl, mylist, myview, sViewType, vnQuery, vnFields, vnRowLimit, false);
if (!retcode)
{
this.SetStatus("creation of view failed....");
}
else
{
this.SetStatus("view created");
}
}
}
else
{
this.SetStatus("Error on finding template view guid");
retcode = false;
}
this.SetStatus("");
}
catch (Exception exception1)
{
MessageBox.Show(exception1.Message, "Error loading view info", MessageBoxButtons.OK);
this.SetStatus("Error loading view info");
retcode = false;
}
finally
{
views.Dispose();
}
}
private Boolean CreateView(String myUrl, String myList, String myView, String myViewType, XmlNode myQuery, XmlNode myFields, XmlNode myRowLimit, Boolean myDefaultView)
{
Boolean retcode = true;
SetStatus("Initialize webservice...");
WS_Views.Views views = new WS_Views.Views();
if (chk_DefaultLoggin.Checked)
views.Credentials = System.Net.CredentialCache.DefaultCredentials;
else
{
views.PreAuthenticate = true;
views.Credentials = new System.Net.NetworkCredential(txt_UserID.Text, txt_PassWord.Text, txt_domain.Text);
}
views.Url = myUrl + @"/_vti_bin/Views.asmx";
try
{
XmlNode retnode = views.AddView(myList, myView, myFields, myQuery, myRowLimit, myViewType, myDefaultView);
SetStatus("View " + myUrl + myList + myView + "created..");
}
catch (Exception exeption1)
{
this.SetStatus("Error on Create View" + exeption1.Message);
retcode = false;
}
finally
{
views.Dispose();
}
return retcode;
}
Geen opmerkingen:
Een reactie posten