Friday, November 14, 2014

How to programmatically change value of a People Picker field or any other field through C# code in Infopath Form Library.

For Simple we can do it without using any namespace just with filed names :

private static void WithoutNameSpace(SPListItem objSPListItem)
        {
                            XmlDocument xml = new XmlDocument();

                            //Open XML file and load it into XML document
                            using (Stream s = objSPListItem.File.OpenBinaryStream())
                            {
                                xml.Load(s);
                            }

                            //Do your stuff with xml here. This is just an example of setting a field
                            Console.WriteLine(xml.InnerXml);

                            
                            XmlNodeList nodes = xml.GetElementsByTagName("my:IPCountry");
                            foreach (XmlNode node in nodes)
                            {
                                Console.WriteLine("Got value.." + node.InnerText);
                                node.InnerText = "Ireland";
                            }
                                
                            //Get binary data for new XML
                            byte[] xmlData = System.Text.Encoding.UTF8.GetBytes(xml.OuterXml);

                            using (MemoryStream ms = new MemoryStream(xmlData))
                            {
                                //Write data to SharePoint XML file
                                objSPListItem.File.SaveBinary(ms);
                            }
                            Console.WriteLine("Changes Saved");
                            Console.Read();
        }

For special type of fields like people picker first get the namespace that field and then set each attribute inside that field.

private static void WithNameSpaceApproach(SPList objSPListParent)
        {
            SPFile spf;
            XmlDocument doc;
            XmlNamespaceManager nsm;
            SPListItem itm = objSPListParent.GetItemById(6);
            spf = itm.File;
            try
            {

                doc = new XmlDocument();
                doc.Load(spf.OpenBinaryStream());
                nsm = new XmlNamespaceManager(doc.NameTable);
                foreach (XmlAttribute att in doc.DocumentElement.Attributes)
                {
                    if (att.Prefix == "xmlns")
                    {
                        nsm.AddNamespace(att.LocalName, att.Value);
                    }
                }
//Print all the old values..
                Console.WriteLine(doc.SelectSingleNode("//my:ResponsiblePerson" + "/pc:Person/pc:AccountId", nsm).InnerText.ToString());
                Console.WriteLine(doc.SelectSingleNode("//my:ResponsiblePerson" + "/pc:Person/pc:DisplayName", nsm).InnerText.ToString());
                Console.WriteLine(doc.SelectSingleNode("//my:ResponsiblePerson" + "/pc:Person/pc:AccountType", nsm).InnerText.ToString());
                spf.CheckOut();
                doc.SelectSingleNode("//my:ResponsiblePerson" + "/pc:Person/pc:AccountId", nsm).InnerText = @"INT\IJohnson";
                doc.SelectSingleNode("//my:ResponsiblePerson" + "/pc:Person/pc:DisplayName", nsm).InnerText = "Ian Johnson";
                doc.SelectSingleNode("//my:ResponsiblePerson" + "/pc:Person/pc:AccountType", nsm).InnerText = "User";
                spf.SaveBinary(new MemoryStream(Encoding.UTF8.GetBytes(doc.OuterXml)));
                spf.CheckIn("Updated by the UpdateInfoPathItems utility!");
                Console.WriteLine("Updated...");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                spf.CheckIn("Updated by the UpdateInfoPathItems utility!");

            }
        }

No comments:

Post a Comment