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


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);
        }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值