Friday, December 6, 2013

Check if the Site Column Exits in SiteCollection

    /// <summary>
        /// Ensures that the site column exists.
        /// </summary>
        /// <param name="web">Rootweb container of site columns</param>
        /// <param name="fieldname">Name of the site column</param>
        /// <param name="showInNewAndEditForm">Boolean determines whether to field exits</param>
        /// <returns></returns>
        public bool EnsureSiteColumn(string spsite, string fieldname)
        {
         
            bool FieldExits=false;
            using (SPSite site = new SPSite(spsite))
            {
                using (SPWeb web = site.RootWeb)
                {
                    try
                    {
                        web.AllowUnsafeUpdates = true;

                        SPField fieldExists = null;
                        if (web.Fields.ContainsField(fieldname))
                            fieldExists = web.Fields[fieldname];

                        if (fieldExists == null)
                        {
                            FieldExits = false;
                            //Site Column Not Found
                        }
                        else
                        {
                            FieldExits = true;
                            //Site Column Found
                        }
                    }
                    catch(Exception ex)
                    {
                       throw ex;
                    }
                    finally
                    {
                        web.AllowUnsafeUpdates = false;
                    }
                }
            }


            return FieldExits;
        }


            return FieldExits;
        }

No comments:

Post a Comment