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

本文详细介绍了如何使用客户端对象模型从SPsite2010获取文档列表中项目的元数据值,并将这些元数据保存到XML文件中。包括使用配置XML文档文件获取元数据值并将其保存到元数据XML文件的过程。


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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值