SharePoint – AddingFieldAsXML

nHi,

n

n

n

nI was basically trying to hook up a SharePoint Field to the one of document library of my site , I know there are different approaches like using UI, writing code.. that one can follow to add desired field directly

n

nbut here is something new I came across , Adding Field as XML..:)

n

nwhat this approach is all about? this is like a creating a Site Column (Field Element) by using feature (I hope I am right)

n

nwe can  create a field on the fly and provide some attributes like ID, Type, DisplayName, Name, Required….

n

nthis is what I did for attaching a Note Type Field to one of doc Lib

n

n

n

nusing (SPSite site = new SPSite(http://YourSite))

n

n{

n

n   using (SPWeb web = site.RootWeb)

n

n   {

n

n      SPList docList = web.Lists[“Documents”];

n

n      if (docList != null)

n

n      {

n

n         XmlDocument doc = new XmlDocument();

n

n         XmlElement xmlElement = doc.CreateElement(“Field”);

n

n         xmlElement.SetAttribute(“ID”, “0F7A6C90-8715-4aa7-90FE-3491DC8953C7”);   //ID can be any GUID        

n

n         xmlElement.SetAttribute(“Type”, “Note”);

n

n         xmlElement.SetAttribute(“Name”, “UserNote”);

n

n         xmlElement.SetAttribute(“DisplayName”, “UserNote”);

n

n         xmlElement.SetAttribute(“Required”, “FALSE”);

n

n

n

n         docList.Fields.AddFieldAsXml(xmlElement.OuterXml);

n

n         docList.Update();

n

n                           

n

n         Console.WriteLine(“Added Field As XML”);

n

n      }

n

n   }

n

n}

n

n

Leave a Comment