Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 25064

how to get Field Id Guid Programmatically

$
0
0

Hi All,

I reading the sample of creating content type programatically from (SharePoint 2010 101 Code Samples) project.

I stopped in a line of code

        //This GUID uniquely idenitifies the custom field
        public static readonly Guid MyFieldId = new Guid("B9EE12F5-B540-4F11-A21B-68A524014C45");

        //This is the XML used to create the field
        public static readonly string MyFieldDefXml =
             "<Field ID=\"{B9EE12F5-B540-4F11-A21B-68A524014C43}\"" +
             " Name=\"ContosoProductName\" StaticName=\"ContosoProductName\"" +
             " Type=\"Text\" DisplayName=\"Contoso Product Name\"" +
             " Group=\"Product Columns\" DisplaceOnUpgrade=\"TRUE\" />";

now my question is, in the new Guid("B9EE12F5-B540-4F11-A21B-68A524014C45"), from where he got this guid ? and why he did not use it any where ?

and for the Field ID ={B9EE12F5-B540-4F11-A21B-68A524014C43}, also from where he got this guid ? and why it did not match the previous guid ?

Thanks, 

here is the complete code

        //This GUID uniquely idenitifies the custom field
        public static readonly Guid MyFieldId = new Guid("B9EE12F5-B540-4F11-A21B-68A524014C45");
        //This is the XML used to create the field
        public static readonly string MyFieldDefXml =
             "<Field ID=\"{B9EE12F5-B540-4F11-A21B-68A524014C43}\"" +
             " Name=\"ContosoProductName\" StaticName=\"ContosoProductName\"" +
             " Type=\"Text\" DisplayName=\"Contoso Product Name\"" +
             " Group=\"Product Columns\" DisplaceOnUpgrade=\"TRUE\" />";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            //Get references to the site and web, ensuring correct disposal
            using (SPSite site = (SPSite)properties.Feature.Parent)
            {
                using (SPWeb web = site.RootWeb)
                {
                    //Check if the custom field already exists.
                    if (web.AvailableFields.Contains(MyFieldId) == false)
                    {
                        //Create the new field
                        web.Fields.AddFieldAsXml(MyFieldDefXml);
                        web.Update();
                    }
                    //Check if the content type already exists
                    SPContentType myContentType = web.ContentTypes["Product Announcement Content Type"];
                    if (myContentType == null)
                    {
                        //Our content type will be based on the Annoucement content type
                        SPContentType announcementContentType = web.AvailableContentTypes[SPBuiltInContentTypeId.Announcement];

                        //Create the new content type
                        myContentType = new SPContentType(announcementContentType, web.ContentTypes, "Product Announcement Content Type");
                       
                        //Add the custom field to it
                        SPFieldLink newFieldLink = new SPFieldLink(web.AvailableFields["Contoso Product Name"]);
                        myContentType.FieldLinks.Add(newFieldLink);
                       
                        //Add the new content type to the site
                        web.ContentTypes.Add(myContentType);
                        web.Update();

                        //Add it to the Announcements list
                        SPList annoucementsList = web.Lists["Announcements"];
                        annoucementsList.ContentTypesEnabled = true;
                        annoucementsList.ContentTypes.Add(myContentType);
                        annoucementsList.Update();
                    }
                }
            }
        }


Viewing all articles
Browse latest Browse all 25064

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>