您的位置:首页 > 移动开发 > Objective-C

Get the metadata value using client object model from the sharepoint 2010

2013-12-20 20:48 489 查看
This blog will continue
The steps of migrating metadata from SP site 2010 to SP site 2013, and the below content will show you how to get the metadata value of the item in docuemnt list from the SP site 2010.

1. Get the metadata value using client object model.

2. Retrive the list items with the list and save all items metadata value into xml file.

3.
Base operation about xml file

Here is the code about getting metadata value:

/// <summary>
/// get the metadata value from shareoint 2010 site via the config xml document file and save the metada to the metada xml file.
/// </summary>
/// <param name="siteUrl"></param>
/// <param name="webUrl"></param>
/// <param name="listName"></param>
/// <param name="metadataName"></param>
public void getMetadata(string siteUrl, string webUrl, string listName, string metadataName, string xmlFile)
{
ClientContext client = new ClientContext(siteUrl + webUrl);
Web clientWeb = client.Web;
client.Load(clientWeb);
client.ExecuteQuery();
ListCollection clientLists = clientWeb.Lists;
List clientList = clientLists.GetByTitle(listName);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View Scope='RecursiveAll'><Query><Where>" +
"<Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq>" +
"</Where></Query></View>";
ListItemCollection collListItem = clientList.GetItems(camlQuery);
client.Load(collListItem,
items => items.Include(
item => item[metadataName],
item => item["LinkFilename"],
item => item["FileRef"]));
client.ExecuteQuery();
Field field = clientList.Fields.GetByInternalNameOrTitle(metadataName);
Microsoft.SharePoint.Client.Taxonomy.TaxonomyField txField = client.CastTo<Microsoft.SharePoint.Client.Taxonomy.TaxonomyField>(field);
client.Load(txField);
client.ExecuteQuery();

MetadataXML xml = new MetadataXML();
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(xmlFile);

foreach (ListItem oListItem in collListItem)
{
if (txField.AllowMultipleValues)
{
string name = oListItem["LinkFilename"].ToString();
string url = oListItem["FileRef"].ToString();
string folderUrl = url.Replace("/" + name, "");

if (((object[])(oListItem[metadataName])).Length < 1)
{
xml.addDocument(xmlFile, webUrl, listName, metadataName, folderUrl, name, "");
}
for (int i = 0; i < ((object[])(oListItem[metadataName])).Length; i++)
{
//string metadata = oListItem[metadataName].ToString().Substring(0, oListItem[metadataName].ToString().IndexOf("|"));
string metaDataLabel = ((object[])(oListItem[metadataName]))[i].ToString().Substring(0, ((object[])(oListItem[metadataName]))[i].ToString().IndexOf("|"));
xml.addDocument(xmlFile, webUrl, listName, metadataName, folderUrl, name, metaDataLabel);
}
}
else
{
string name = oListItem["LinkFilename"].ToString();
string url = oListItem["FileRef"].ToString();
string folderUrl = url.Replace("/" + name, "");
if (oListItem[metadataName] != null && oListItem[metadataName].ToString() != "")
{
//(Microsoft.SharePoint.Client.Taxonomy.TaxonomyFieldValue)(oListItem[metadataName])).Label
// only get data from 2010
string metadata = oListItem[metadataName].ToString().Substring(0, oListItem[metadataName].ToString().IndexOf("|"));
xml.addDocument(xmlFile, webUrl, listName, metadataName, folderUrl, name, metadata);
}
else
{
xml.addDocument(xmlFile, webUrl, listName, metadataName, folderUrl, name, "");
}
}
}
}


Here is the code about save metadata value into xml file

public void addDocument(string xmlFilePath, string webUrl, String documentName, string metadataName, string relativeUrl, string fileName, string metadata)
{
addWebTag(xmlFilePath, webUrl);
addDocumentTag(xmlFilePath, webUrl, documentName, metadataName);
addFolderTag(xmlFilePath, webUrl, documentName, metadataName, relativeUrl);

XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(xmlFilePath);
XmlNode rootNode = myXmlDoc.SelectSingleNode("Site");
XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
XmlNodeList webNodes = rootNode.ChildNodes;
foreach (XmlNode webNode in firstLevelNodeList)
{
if (webNode.Attributes["Url"].Value.Equals(webUrl))
{
XmlNodeList documentNodes = webNode.ChildNodes;
foreach (XmlNode documentNode in documentNodes)
{
if (documentNode.Attributes["Name"].Value.Equals(documentName) && documentNode.Attributes["Metadata"].Value.Equals(metadataName))
{
XmlNodeList folderNodes = documentNode.ChildNodes;
foreach (XmlNode folderNode in folderNodes)
{
if (folderNode.Attributes["RelativeUrl"].Value.Equals(relativeUrl))
{
XmlElement newElement = myXmlDoc.CreateElement("Item");
newElement.SetAttribute("Name", fileName);
newElement.InnerText = metadata;
folderNode.AppendChild(newElement);
}
}
}
}
}
}
myXmlDoc.Save(xmlFilePath);
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: