Archive for December, 2011

SharePoint 2101: Migrate List Views to SharePoint Online

Scenario:

We need to migrate List Views from SharePoint 2010 Server to SharePoint Online. We can use Web Services or Client Model. We decided to use Views Web Service.

Resolution:

First, we created a class which stores view information.

public class ListViewInfo

{

public ListViewInfo()

{

}

[DataMember]

public string listName { get; set; }

[DataMember]

public string viewName { get; set; }

[DataMember]

public string viewGUID { get; set; }

[DataMember]

public string openApplicationExtension { get; set; }

[DataMember]

public XmlNode viewFields { get; set; }

[DataMember]

public XmlNode viewQuery { get; set; }

[DataMember]

public XmlNode rowLimit { get; set; }

[DataMember]

public string type { get; set; }

[DataMember]

public bool makeViewDefault { get; set; }

[DataMember]

public XmlNode viewHeader { get; set; }

[DataMember]

public XmlNode viewBody { get; set; }

[DataMember]

public XmlNode viewFooter { get; set; }

[DataMember]

public XmlNode viewEmpty { get; set; }

[DataMember]

public XmlNode rowLimitExceeded { get; set; }

[DataMember]

public XmlNode viewToolBar { get; set; }

[DataMember]

public XmlNode viewAggregations { get; set; }

[DataMember]

public XmlNode viewFormat { get; set; }

}

We created two methods to get views information and to create views.

public static List<ListViewInfo> GetViewDetails(string listUrl)

{

List<ListViewInfo> listViewList = new List<ListViewInfo>();

ListViewInfo listView = new ListViewInfo();

string viewXML = string.Empty;

XmlDocument doc = new XmlDocument();

using (SPSite spSite = new SPSite(“SiteURL”))

{

using (SPWeb spWeb = spSite.OpenWeb())

{

SPList spList = spWeb.GetList(listUrl);

SPViewCollection spViewColl = spList.Views;

foreach (SPView spView in spViewColl)

{

viewXML = spView.HtmlSchemaXml;

doc.LoadXml(viewXML);

XmlNode root = doc.FirstChild;

listView.listName = spList.Title;

listView.viewName = root.Attributes[“DisplayName”].Value;

listView.viewGUID = root.Attributes[“Name”].Value;

listView.viewFields = root.SelectSingleNode(“/View/ViewFields”);

listView.viewQuery = root.SelectSingleNode(“/View/Query”);

listView.rowLimit = root.SelectSingleNode(“/View/RowLimit”);

listView.type = spView.Type;

listView.makeViewDefault = spView.DefaultView;

listView.viewHeader = root.SelectSingleNode(“/View/ViewHeader”);

listView.viewFooter = root.SelectSingleNode(“/View/ViewFooter”);

listView.viewBody = root.SelectSingleNode(“/View/ViewBody”);

listView.viewEmpty = root.SelectSingleNode(“/View/ViewEmpty”);

listView.viewToolBar = root.SelectSingleNode(“/View/ViewToolbar”);

listView.viewAggregations = root.SelectSingleNode(“/View/Aggregations”);

listView.viewFormat = root.SelectSingleNode(“/View/Formats”);

listView.openApplicationExtension = spView.OpenApplicationExtension;

listViewList.Add(listView);

listView = new ListViewInfo();

doc.RemoveAll();

}

}

}

return listViewList;

}

public static void createOrUpdateListView(List<ListViewInfo> listViewList)

{

ViewSvr.Views viewSrv = new ViewSvr.Views();

viewSrv.Credentials = System.Net.CredentialCache.DefaultCredentials;

viewSrv.Url = “http://SiteURL/_vti_bin/Views.asmx&#8221;;

if (listViewList.Count > 0)

{

string listName = listViewList[0].listName;

XmlNode viewColl = viewSrv.GetViewCollection(listName);

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.LoadXml(“<root>” + viewColl.InnerXml + “</root>”);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);

nsmgr.AddNamespace(“XYZ”, “http://schemas.microsoft.com/sharepoint/soap/&#8221;);

foreach (XPSListView listView in listViewList)

{

XmlNode foundView = xmlDoc.SelectSingleNode(@”/root/XYZ:View[@DisplayName='” + listView.viewName + “‘]”, nsmgr);

if (foundView == null)

{

//add view

XmlNode nv = viewSrv.AddView(listView.listName, listView.viewName, listView.viewFields, listView.viewQuery, listView.rowLimit, listView.type, listView.makeViewDefault);

}

else

{

//update view

XmlNode uv = viewSrv.UpdateViewHtml2(listView.listName, foundView.Attributes[“Name”].Value, null, listView.viewToolBar, listView.viewHeader, listView.viewBody, listView.viewFooter, listView.viewEmpty, listView.rowLimitExceeded, listView.viewQuery,listView.viewFields, listView.viewAggregations,listView.viewFormat,listView.rowLimit, listView.openApplicationExtension);                    }

foundView = null;

}

}

}

P.S. We have used UpdateViewHtml2 Method to update the view but not able to find viewProperties. Please let me know if anyone find the mapping.

Also, please add web reference of Views Web Service in your project and replace following line with your web reference

ViewSvr.Views viewSrv = new ViewSvr.Views();

Advertisements

Leave a Comment