Update-Delete-Insert an item to a different SharePoint Farm

В своей работе я столкнулся с необходимостью передавать данные из одной фермы sharepoint в список, который находится в другой ферме. Сделать можно используя web service (есть и другие способы, но они не так удобны). Для этого я создал класс SPService. В проект нужно добавить Web References https://yourportalname.ru/_vti_bin/Lists.asmx и назвать его "ListService".

Код класса:

public class SPService
    {
        public string WebUrl { get; set; }
        public NetworkCredential Credential { get; set; }
        public enum MethodType { New, Update, Delete }

        private ListService.Lists SpLists;
        private string Lists = "/_vti_bin/Lists.asmx";

        public SPService()
        {
            SpLists = new ListService.Lists();
            this.WebUrl = "https://default.ru/";
            this.Credential = new NetworkCredential("defaultname", "defaultpassword");
            UpdateServiceVariables();
        }

        private void UpdateServiceVariables()
        {
            SpLists.Url = this.WebUrl.TrimEnd('/') + Lists;
            SpLists.Credentials = Credential;
            SpLists.PreAuthenticate = true;
        }

       
        public void UpdateListItem(MethodType cmd, string listName, Dictionary<string, string> fields)
        {
            try
            {
                UpdateServiceVariables();

                #region Getting GUID of List and View

                System.Xml.XmlNode ndListView = SpLists.GetListAndView(listName, "");
                string strListID = ndListView.ChildNodes[0].Attributes["Name"].Value;
                string strViewID = ndListView.ChildNodes[1].Attributes["Name"].Value;

                #endregion

                #region Creating XML Document

                System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
                System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");

                elBatch.SetAttribute("OnError", "Continue");
                elBatch.SetAttribute("ListVersion", "1");
                elBatch.SetAttribute("ViewName", strViewID);

                #endregion

                #region "Generate Batch Command"

                string strBatch = String.Format("<Method ID='1' Cmd='{0}'>", Enum.GetName(typeof(MethodType), cmd));
                foreach (KeyValuePair<string, string> field in fields)
                {
                    strBatch += String.Format("<Field Name='{0}'>{1}</Field>", field.Key, field.Value);
                }
                strBatch += "</Method>";
                elBatch.InnerXml = strBatch;

                #endregion

                SpLists.UpdateListItems(strListID, elBatch);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
    }

Как это работает:

            SPService ss = new SPService();
            ss.WebUrl = "https://youportalname.ru/DB/";
            ss.Credential = new NetworkCredential("xxxxx", "xxxxxx");

            ss.UpdateListItem(SPService.MethodType.Update, "test",
               new Dictionary<string, string>()
                       {
                           { "ID", "3" }, 
                           { "Choice", "Вариант 3" },
                           { "RichText", "<![CDATA[<br/><a href=\"#\">ссылка</a> <b>тест</b>]]>" },
                           { "Link", "10;#Головной офис" },
                           { "Date",  SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Today) },
                       });

 

 

Добавить комментарий

Loading