Tuesday, 3 March 2015

SharePoint Utility Part1

Here you will find the code for
AddListEventReceiver
RemoveListEventReceiver
AddContentTypeEventReceiver
RemoveContentTypeEventReceiver
CreateNewWeb
SetStringFieldValue
SetHyperlinkFieldValue
GetListItemBySomefieldvalue - not sure
GetLookupValue
IfItemExist - Check if current item exist in list
GetUser
GetAllUser
GetListFieldInternalName - Get Field Internal Name
CreateList
GetSiteColumn
AddContentTypeToList
AddParentContentTypeToList
SetDefaultContentType
IsContentTypeAttached
AddLookupFieldToContentType
AddSharePointGroupsToWeb
GetSiteGroup
ActivateSiteCollectionFeature
ActivateWebApplicationFeature
ActivateWebFeature
DeactivateFeature
CreateLookupSiteColumn
CreateSingleLineColumn
CreateMultiLineColumn
CreateMultiLineColumnAsPlainText
CreateDateTimeColumn
CreateDateTimeColumnForCurrentDate
CreateNumberColumn
CreateChoiceColumn
CreateCalculatedColumn
CreateBooleanColumn
CreateUserColumn
CreateHyperlinkColumn
CreateLookupColumn
AddSiteColumn
AddSiteColumnReadOnly
AddSiteColumn
SetWelcomePage
AddListViewWebPartToPage
AddCEWebPartToPage



using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Publishing.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls.WebParts;
using System.Xml;

namespace Common.SharePointUtil
{
    public class SharePointUtility
    {
        #region Bind/Remove Event Receivers

        /// <summary>
        /// Add EventReceiver on List
        /// </summary>
        public static void AddListEventReceiver(SPWeb spweb, string listName, string receiverAssemblyName, string className, SPEventReceiverType eventType)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(spweb.Site.ID))
                    {
                        using (SPWeb web = site.OpenWeb(spweb.ID))
                        {
                            SPList list = web.Lists.TryGetList(listName);

                            if (list != null)
                            {
                                bool found = false;

                                // Check if Event Receiver already exist
                                foreach (SPEventReceiverDefinition e in list.EventReceivers)
                                {
                                    if (string.Compare(e.Class, className, true) == 0 && e.Type == eventType)
                                    {
                                        found = true;
                                        break;
                                    }
                                }

                                // If event receiver not found
                                if (!found)
                                {
                                    web.AllowUnsafeUpdates = true;
                                    SPEventReceiverDefinition eventDefinition = list.EventReceivers.Add();
                                    eventDefinition.Assembly = receiverAssemblyName;
                                    eventDefinition.Class = className;
                                    eventDefinition.SequenceNumber = 1000;
                                    eventDefinition.Type = eventType;
                                    eventDefinition.HostId = list.ID;
                                    eventDefinition.HostType = SPEventHostType.List;
                                    eventDefinition.Update();
                                    list.Update();
                                    web.Update();
                                    web.AllowUnsafeUpdates = false;
                                }
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
        }

        /// <summary>
        /// Remove EventReceiver on List
        /// </summary>
        public static void RemoveListEventReceiver(SPWeb web, string listName, string receiverAssemblyName, string className)
        {
            try
            {
                SPList list = web.Lists.TryGetList(listName);

                if (list != null)
                {
                    //validate event receivers
                    if (list.EventReceivers != null && list.EventReceivers.Count > 0)
                    {
                        List<SPEventReceiverDefinition> events = list.EventReceivers.Cast<SPEventReceiverDefinition>()
                            .Where(x => (string.Compare(x.Class, className, true) == 0) && (string.Compare(x.Assembly, receiverAssemblyName, true) == 0)).ToList();

                        foreach (SPEventReceiverDefinition e in events)
                        {
                            e.Delete();
                        }
                    }
                }
            }
            catch (Exception ex)
            {

                ExceptionManager.HandleException(ex);
            }
        }

        /// <summary>
        /// Bind Event Receiver to Content Type
        /// </summary>
        /// <param name="web"></param>
        /// <param name="contentTypeName"></param>
        /// <param name="receiverAssemblyName"></param>
        /// <param name="className"></param>
        /// <param name="eventType"></param>
        public static void AddContentTypeEventReceiver(SPWeb web, string contentTypeName, string receiverAssemblyName, string className, SPEventReceiverType eventType)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(web.Site.ID))
                    {
                        using (SPWeb SpWeb = site.OpenWeb(web.ID))
                        {
                            SPContentType contentType = web.ContentTypes[contentTypeName];

                            if (contentType != null)
                            {
                                bool found = false;

                                // Check if Event Receiver already exist
                                foreach (SPEventReceiverDefinition e in contentType.EventReceivers)
                                {
                                    if (string.Compare(e.Class, className, true) == 0 && e.Type == eventType)
                                    {
                                        found = true;
                                        break;
                                    }
                                }

                                // If event receiver not found
                                if (!found)
                                {
                                    SpWeb.AllowUnsafeUpdates = true;
                                    SPEventReceiverDefinition eventDefinition = contentType.EventReceivers.Add();
                                    eventDefinition.Assembly = receiverAssemblyName;
                                    eventDefinition.Class = className;
                                    eventDefinition.SequenceNumber = 1000;
                                    eventDefinition.Type = eventType;
                                    eventDefinition.HostType = SPEventHostType.ContentType;
                                    eventDefinition.Update();
                                    contentType.Update(true);
                                    SpWeb.Update();
                                    SpWeb.AllowUnsafeUpdates = false;
                                }
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
        }

        /// <summary>
        /// Remove Content Type Event Receiver
        /// </summary>
        /// <param name="web"></param>
        /// <param name="listName"></param>
        /// <param name="receiverAssemblyName"></param>
        /// <param name="className"></param>
        public static void RemoveContentTypeEventReceiver(SPWeb web, string contentTypeName, string receiverAssemblyName, string className)
        {
            try
            {
                SPContentType contentType = web.ContentTypes[contentTypeName];

                if (contentType != null)
                {
                    //validate event receivers
                    if (contentType.EventReceivers != null && contentType.EventReceivers.Count > 0)
                    {
                        List<SPEventReceiverDefinition> events = contentType.EventReceivers.Cast<SPEventReceiverDefinition>()
                            .Where(x => (string.Compare(x.Class, className, true) == 0) && (string.Compare(x.Assembly, receiverAssemblyName, true) == 0)).ToList();

                        foreach (SPEventReceiverDefinition e in events)
                        {
                            e.Delete();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
        }

        #endregion

        #region Create New SPWeb

        public static SPWeb CreateNewWeb(string title, SPWeb web, string templateName)
        {
            SPWeb newWeb = null;

            try
            {
                if (web != null)
                {
                    web.AllowUnsafeUpdates = true;

                    //Set the LCID to get the template from the template gallery
                    UInt32 nLocalID = Convert.ToUInt32(1033);

                    //Get the Desired Web template
                    SPWebTemplateCollection webTemplates = web.Site.RootWeb.GetAvailableWebTemplates(nLocalID);

                    var template = (from SPWebTemplate t in webTemplates
                                    where t.Name == templateName || t.Title == templateName
                                    select t).FirstOrDefault();

                    //Create new site
                    if (template != null)
                    {
                        newWeb = web.Webs.Add(title, title, string.Empty, nLocalID, template, false, false);
                        web.AllowUnsafeUpdates = false;
                    }
                    else
                    {
                        SharePointLogManager.Log("PPAP System : Unable to find " + templateName);
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
                return null;
            }

            return newWeb;
        }

        #endregion

        #region Set SPField value

        public static bool SetStringFieldValue(string column, string value, SPList list)
        {
            try
            {
                return true;
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
                return false;
            }
        }

        public static bool SetHyperlinkFieldValue(string column, string url, string description, SPListItem item)
        {
            try
            {
                SPFieldUrlValue urlValue = new SPFieldUrlValue();
                urlValue.Url = url;
                urlValue.Description = description;
                item[column] = urlValue;
                item.Update();
                return true;
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
                return false;
            }
        }

        #endregion

        #region Get SPList Item

        public static SPListItem GetListItemByDieNo(string dieNo, string listName, SPWeb web)
        {
            try
            {
                SPList list = web.Lists.TryGetList(listName);

                if (list == null)
                    return null;

                SPQuery query = new SPQuery();

                if (list.Fields.ContainsField(Constants.COL_DIE))
                    query.Query = string.Format(Constants.QUERY_PPAP_LIST_GET_ITEM_BY_DIE_NO, dieNo);
                else if (list.Fields.ContainsField(Constants.COL_DIE_NO))
                    query.Query = string.Format(Constants.QUERY_NPD_LIST_GET_ITEM_BY_DIE_NO, dieNo);

                SPListItemCollection items = list.GetItems(query);

                return items[0];
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
                return null;
            }
        }
        /// <summary>
        /// THis methods returns lookupid/lookupvalue of the field
        /// </summary>
        /// <param name="item"></param>
        /// <param name="requireId"></param>
        /// <returns></returns>
        public static string GetLookupValue(object item, bool requireId)
        {
            string value = string.Empty;

            try
            {
                if (item != null)
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        SPFieldLookupValue lookupField = new SPFieldLookupValue(Convert.ToString(item));
                        if (lookupField != null)
                        {

                            if (requireId)
                                value = lookupField.LookupId.ToString();
                            else
                                value = lookupField.LookupValue;
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }

            return value;
        }

        /// <summary>
        /// Check if current item exist in list
        /// </summary>
        /// <param name="item"></param>
        /// <param name="listName"></param>
        /// <returns></returns>
        public static bool IfItemExist(SPListItem item, SPList list)
        {
            try
            {
                SPQuery query = new SPQuery(list.DefaultView);
                query.Query = string.Format(Constants.QUERY_IF_ITEM_EXIST, item.Title);
                SPListItemCollection items = list.GetItems(query);

                if (items != null && items.Count > 0)
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }

            return false;
        }
        public static SPUser GetUser(SPListItem item, SPField userField)
        {
            // System.Diagnostics.Debugger.Break();
            SPUser currentUser = null;
            try
            {
                string currentValue = Convert.ToString(item[userField.Title]);
                SPFieldUser field = userField as SPFieldUser;
                SPFieldUserValue fieldValue = field.GetFieldValue(currentValue) as SPFieldUserValue;
                if (fieldValue != null)
                    currentUser = fieldValue.User;
            }
            catch (Exception ex)
            {

                ExceptionManager.HandleException(ex);
            }

            return currentUser;
        }
        public static List<SPUser> GetAllUser(SPListItem item, SPField userField)
        {
            // System.Diagnostics.Debugger.Break();
            List<SPUser> userlist = new List<SPUser>();
            SPUser currentUser = null;
            try
            {
                string currentValue = Convert.ToString(item[userField.Title]);
                SPFieldUser field = userField as SPFieldUser;
                if (field != null)
                {
                    SPFieldUserValueCollection fieldValue = field.GetFieldValue(currentValue) as SPFieldUserValueCollection;
                    if (fieldValue != null)
                    {
                        foreach (SPFieldUserValue fielduser in fieldValue)
                        {
                            if (fielduser != null)
                                currentUser = fielduser.User;
                            userlist.Add(currentUser);
                        }
                    }
                }
            }
            catch (Exception ex)
            {

                ExceptionManager.HandleException(ex);
            }

            return userlist;
        }
        #endregion

        #region Get Field Internal Name

        public static string GetListFieldInternalName(SPWeb web, string lstName, string fieldDisplayname)
        {
            string internalName = null;
            SPList list = null;

            try
            {
                list = web.Lists[lstName];

                foreach (SPField spField in list.Fields)
                {
                    if (string.Compare(fieldDisplayname, spField.Title, StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        internalName = spField.InternalName;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex); ;
            }

            return internalName;
        }
        #endregion

        #region Create SPList

        /// <summary>
        /// Creates list based on Schema XML
        /// </summary>
        /// <param name="listInstanceName"></param>
        /// <param name="listSchemaFile"></param>
        public static bool CreateList(SPWeb web, string listInstanceName, Dictionary<string, SPFieldType> listColumns)
        {
            string fieldInternalName = string.Empty;
            SPField siteColumnField = null;
            SPList currentList = null;
            SPView defaultView = null;
            SPField newField = null;


            try
            {
                #region Add List/Library

                // Get currently aaded list
                currentList = web.Lists.TryGetList(listInstanceName);

                if (currentList == null)
                    return false;

                #endregion

                #region Add Columns

                defaultView = currentList.DefaultView;

                // get fields Collection for current list
                foreach (KeyValuePair<string, SPFieldType> field in listColumns)
                {
                    if (!currentList.Fields.ContainsField(field.Key))
                    {
                        try
                        {
                            #region Add Site Column

                            siteColumnField = GetSiteColumn(web, field.Key);

                            if (siteColumnField != null)
                            {
                                fieldInternalName = currentList.Fields.Add(siteColumnField);
                            }

                            #endregion

                            #region Add new column

                            else
                            {
                                switch (field.Value)
                                {
                                    // DateTime
                                    case SPFieldType.DateTime:
                                        #region DateTime
                                        fieldInternalName = currentList.Fields.Add(field.Key, field.Value, false);

                                        SPFieldDateTime dateTimeField = currentList.Fields.GetFieldByInternalName(fieldInternalName) as SPFieldDateTime;
                                        if (dateTimeField != null)
                                        {
                                            dateTimeField.DisplayFormat = SPDateTimeFieldFormatType.DateOnly;
                                            dateTimeField.Required = false;
                                            dateTimeField.ShowInNewForm = true;
                                            dateTimeField.ShowInEditForm = true;
                                            dateTimeField.ShowInDisplayForm = true;
                                            dateTimeField.Update();
                                        }
                                        #endregion
                                        break;

                                    // Note
                                    case SPFieldType.Note:
                                        #region Note
                                        fieldInternalName = currentList.Fields.Add(field.Key, field.Value, false);

                                        SPFieldMultiLineText multiLineTextField = currentList.Fields.GetFieldByInternalName(fieldInternalName) as SPFieldMultiLineText;
                                        if (multiLineTextField != null)
                                        {
                                            multiLineTextField.Required = false;
                                            multiLineTextField.ShowInNewForm = true;
                                            multiLineTextField.ShowInEditForm = true;
                                            multiLineTextField.ShowInDisplayForm = true;
                                            multiLineTextField.Update();
                                        }
                                        #endregion
                                        break;

                                    // Choice
                                    case SPFieldType.Choice:
                                        #region Choice
                                        fieldInternalName = currentList.Fields.Add(field.Key, field.Value, false);

                                        SPFieldChoice choiceField = currentList.Fields.GetFieldByInternalName(fieldInternalName) as SPFieldChoice;

                                        if (choiceField != null)
                                        {
                                            choiceField.EditFormat = SPChoiceFormatType.Dropdown;
                                            // Clears out the list of options
                                            choiceField.Choices.Clear();
                                            choiceField.Title = field.Key;
                                            choiceField.Required = false;
                                            choiceField.ShowInNewForm = true;
                                            choiceField.ShowInEditForm = true;
                                            choiceField.ShowInDisplayForm = true;
                                            choiceField.Update();
                                        }
                                        #endregion
                                        break;

                                    // Calculated
                                    case SPFieldType.Calculated:
                                        #region Calculated

                                        fieldInternalName = currentList.Fields.Add(field.Key, field.Value, false);
                                        SPFieldCalculated calculatedField = currentList.Fields.GetFieldByInternalName(fieldInternalName) as SPFieldCalculated;

                                        // Set formula if not empty
                                        //if (!string.IsNullOrEmpty(field.Formula))
                                        //    calculatedField.Formula = "=" + field.Formula;
                                        if (calculatedField != null)
                                        {
                                            calculatedField.Title = field.Key;
                                            calculatedField.Required = false;
                                            calculatedField.ShowInNewForm = true;
                                            calculatedField.ShowInEditForm = true;
                                            calculatedField.ShowInDisplayForm = true;
                                            calculatedField.Update();
                                        }
                                        #endregion
                                        break;

                                    // Text
                                    case SPFieldType.Text:
                                        #region Text
                                        fieldInternalName = currentList.Fields.Add(field.Key, field.Value, false);
                                        SPField textField = currentList.Fields.GetFieldByInternalName(fieldInternalName) as SPField;

                                        if (textField != null)
                                        {
                                            textField.ShowInNewForm = true;
                                            textField.ShowInEditForm = true;
                                            textField.ShowInDisplayForm = true;
                                            textField.Update();
                                        }

                                        #endregion
                                        break;

                                    // Boolean
                                    case SPFieldType.Boolean:
                                        #region Boolean
                                        fieldInternalName = currentList.Fields.Add(field.Key, field.Value, false);
                                        SPFieldBoolean boolField = currentList.Fields.GetFieldByInternalName(fieldInternalName) as SPFieldBoolean;

                                        if (boolField != null)
                                        {
                                            boolField.ShowInNewForm = false;
                                            boolField.ShowInEditForm = false;
                                            boolField.ShowInDisplayForm = false;
                                            boolField.Update();
                                        }

                                        #endregion
                                        break;

                                    // User
                                    case SPFieldType.User:
                                        #region User
                                        fieldInternalName = currentList.Fields.Add(field.Key, field.Value, false);
                                        SPFieldUser SPUser = currentList.Fields.GetFieldByInternalName(fieldInternalName) as SPFieldUser;

                                        if (SPUser != null)
                                        {
                                            if (fieldInternalName.Equals(Constants.COL_USERS) || fieldInternalName.ToUpper().Equals(Constants.COL_USERS) || fieldInternalName.ToLower().Equals(Constants.COL_USERS))
                                            {
                                                SPUser.AllowMultipleValues = true;
                                            }
                                            SPUser.Update();
                                        }
                                        #endregion
                                        break;
                                    // Generic
                                    default:
                                        #region Generic
                                        fieldInternalName = currentList.Fields.Add(field.Key, field.Value, false);
                                        #endregion
                                        break;
                                }
                            }

                            #endregion

                            #region Update View

                            if (!string.IsNullOrEmpty(fieldInternalName) && currentList.Fields.ContainsField(fieldInternalName))
                            {
                                newField = currentList.Fields.GetFieldByInternalName(fieldInternalName);

                                if (newField != null)
                                    defaultView.ViewFields.Add(newField.Title);
                            }

                            #endregion
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                    }
                }

                currentList.Update();
                defaultView.Update();
                return true;

                #endregion
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
                return false;
            }
            finally
            {
                defaultView = null;
            }
        }

        /// <summary>
        /// Get available site columns
        /// </summary>
        /// <param name="web"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        private static SPField GetSiteColumn(SPWeb web, string fieldName)
        {
            SPField field = null;

            try
            {
                field = web.AvailableFields.GetField(fieldName);
                return field;
            }
            catch (Exception)
            {
                return null;
            }
        }

        #endregion

        #region Content Type

        public static void AddContentTypeToList(SPWeb web, string contentTypeName, string listName, bool isDefault = false)
        {
            SPContentType contentType = null;
            SPList list = null;
            //SPContentTypeCollection currentOrder = null;
            //List<SPContentType> result = null;

            try
            {
                list = web.Lists.TryGetList(listName);
                contentType = web.ContentTypes.Cast<SPContentType>().Where(x => x.Name.Equals(contentTypeName)).FirstOrDefault();

                if (list != null && contentType != null)
                {
                    if (!IsContentTypeAttached(list, contentType))
                    {
                        list.ContentTypesEnabled = true;
                        //list.Update();
                        list.ContentTypes.Add(contentType);
                        list.Update();
                        if (isDefault)
                        {
                            SetDefaultContentType(list, contentTypeName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                contentType = null;
                list = null;
                //currentOrder = null;
                //result = null;
            }
        }
        /// <summary>
        /// Add content type to parent web
        /// </summary>
        /// <param name="web"></param>
        /// <param name="contentTypeName"></param>
        /// <param name="listName"></param>
        /// <param name="isDefault"></param>
        public static void AddParentContentTypeToList(SPWeb web, string contentTypeName, string listName, bool isDefault = false)
        {
            SPContentType contentType = null;
            SPList list = null;
            //SPContentTypeCollection currentOrder = null;
            //List<SPContentType> result = null;

            try
            {
                list = web.Lists.TryGetList(listName);
                if (web.ParentWeb != null)
                {
                    contentType = web.ParentWeb.ContentTypes.Cast<SPContentType>().Where(x => x.Name.Equals(contentTypeName)).FirstOrDefault();

                    if (list != null && contentType != null)
                    {
                        if (!IsContentTypeAttached(list, contentType))
                        {
                            list.ContentTypesEnabled = true;
                            //list.Update();
                            list.ContentTypes.Add(contentType);
                            list.Update();
                            if (isDefault)
                            {
                                SetDefaultContentType(list, contentTypeName);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                contentType = null;
                list = null;
                //currentOrder = null;
                //result = null;
            }
        }
        /// <summary>
        /// Set the default content ofspecified list
        /// </summary>
        /// <param name="list"></param>
        /// <param name="ContenTypeName"></param>
        private static void SetDefaultContentType(SPList list, string ContenTypeName)
        {
            IList<SPContentType> cTypes = new List<SPContentType>();
            SPFolder root = list.RootFolder;
            cTypes = root.ContentTypeOrder;
            SPContentType cType = cTypes.SingleOrDefault(hd => hd.Name == ContenTypeName);
            int j = cTypes.IndexOf(cType);
            cTypes.RemoveAt(j);
            cTypes.Insert(0, cType);
            root.UniqueContentTypeOrder = cTypes;
            root.Update();
        }
        /// <summary>
        /// Check if Content Type is attached to list
        /// </summary>
        /// <param name="list"></param>
        /// <param name="cType"></param>
        /// <returns></returns>
        private static bool IsContentTypeAttached(SPList list, SPContentType cType)
        {
            bool flag = false;
            SPContentType contentType = null;

            try
            {
                contentType = list.ContentTypes.Cast<SPContentType>().Where(x => x.Name.Equals(cType.Name)).FirstOrDefault();

                if (contentType != null)
                {
                    flag = true;
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                contentType = null;
            }

            return flag;
        }

        public static void AddLookupFieldToContentType(SPWeb web, string siteFieldName, string contentTypename)
        {
            SPField field = null;
            SPFieldLink fieldLink = null;
            SPContentType contentType = null;

            try
            {
                if (web.Fields.ContainsField(siteFieldName))
                {
                    contentType = web.ContentTypes[contentTypename];

                    if (contentType != null)
                    {
                        if (!contentType.Fields.ContainsField(siteFieldName))
                        {
                            field = web.Fields[siteFieldName];

                            if (field != null)
                            {
                                fieldLink = new SPFieldLink(field);
                                contentType.FieldLinks.Add(fieldLink);
                                contentType.Update(true);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
            }
        }

        #endregion

        #region SPGroups

        public void AddSharePointGroupsToWeb(SPWeb eleWeb, string groupName, string groupDescription, SPRoleType role)
        {

            SPGroup innovaAdmin = GetSiteGroup(eleWeb, groupName);
            if (innovaAdmin == null)
            {
                eleWeb.SiteGroups.Add(groupName, eleWeb.AssociatedOwnerGroup, null, groupDescription);
                innovaAdmin = eleWeb.SiteGroups.GetByName(groupName);
            }
            eleWeb.BreakRoleInheritance(false);
            SPRoleDefinition roleDefinition = eleWeb.RoleDefinitions.GetByType(role);
            SPRoleAssignment roleAssigment = new SPRoleAssignment(innovaAdmin);
            roleAssigment.RoleDefinitionBindings.Add(roleDefinition);
            eleWeb.RoleAssignments.Add(roleAssigment);
            eleWeb.Update();

        }

        private SPGroup GetSiteGroup(SPWeb web, string name)
        {
            foreach (SPGroup group in web.SiteGroups)
            {
                if (group.Name.ToLower() == name.ToLower())
                {
                    return group;
                }
            }
            return null;
        }

        #endregion

        #region Bind/Activate Features

        /// <summary>
        ///  Activate RFP Root Site Web scoped Feature
        /// </summary>
        /// <param name="guid">feature guid</param>
        /// <param name="web">SPWeb object</param>
        public static void ActivateSiteCollectionFeature(Guid guid, SPWeb web)
        {
            SPFeature spFeature = null;

            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(web.Site.ID))
                    {
                        site.AllowUnsafeUpdates = true;
                        spFeature = site.Features[guid];

                        if (spFeature == null)
                        {
                            // Activate Feature
                            site.Features.Add(guid, true);
                        }

                        site.AllowUnsafeUpdates = false;

                    }
                });
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                spFeature = null;
            }
        }

        /// <summary>
        ///  Activate RFP Root Site Web scoped Feature
        /// </summary>
        /// <param name="guid">feature guid</param>
        /// <param name="web">SPWeb object</param>
        public static void ActivateWebApplicationFeature(Guid guid, SPWeb web)
        {
            SPFeature spFeature = null;

            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(web.Site.ID))
                    {
                        SPWebApplication webApp = site.WebApplication;
                        spFeature = webApp.Features[guid];

                        if (spFeature == null)
                        {
                            // Activate Feature
                            webApp.Features.Add(guid, true);
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                spFeature = null;
            }
        }

        /// <summary>
        ///  Activate Web scoped Feature
        /// </summary>
        /// <param name="guid">feature guid</param>
        /// <param name="web">SPWeb object</param>
        public static void ActivateWebFeature(Guid guid, SPWeb web)
        {
            SPFeature spFeature = null;
            SPWeb currentWeb = null;

            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(web.Site.ID))
                    {
                        currentWeb = site.AllWebs[web.ID];

                        if (currentWeb != null)
                        {
                            currentWeb.AllowUnsafeUpdates = true;
                            spFeature = site.Features[guid];

                            if (spFeature == null)
                            {
                                // Activate Feature
                                currentWeb.Features.Add(guid, true);
                            }

                            currentWeb.AllowUnsafeUpdates = false;
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                spFeature = null;
                currentWeb = null;
            }
        }

        /// <summary>
        /// Deactivate Web scoped Feature
        /// </summary>
        /// <param name="webTitle"></param>
        /// <param name="guid"></param>
        /// <param name="web"></param>
        public static void DeactivateFeature(Guid guid, SPWeb web)
        {
            SPFeature spFeature = null;

            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    web.AllowUnsafeUpdates = true;
                    spFeature = web.Features[guid];

                    if (spFeature != null)
                    {
                        // Deactivate Feature
                        web.Features.Remove(guid, true);
                    }
                });
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                spFeature = null;
            }
        }

        #endregion

        #region Create Site Columns

        public static bool CreateLookupSiteColumn(SPWeb web, string title, string parentListTitle, string parentColumnTitle, bool required)
        {
            SPList parentList = null;
            string newFieldInternalName = string.Empty;
            SPFieldLookup newField = null;

            try
            {
                parentList = web.Lists.TryGetList(parentListTitle);

                if (parentList == null)
                    return false;

                if (parentList.Fields.ContainsField(parentColumnTitle))
                {
                    newFieldInternalName = web.Fields.AddLookup(title, parentList.ID, required);

                    if (string.IsNullOrEmpty(newFieldInternalName))
                        return false;

                    newField = web.Fields.GetFieldByInternalName(newFieldInternalName) as SPFieldLookup;

                    if (newField == null)
                        return false;

                    newField.Title = title;
                    newField.Group = Constants.GROUP_SITE_COLUMN;
                    newField.LookupField = parentColumnTitle;
                    newField.Update(true);
                }

                return true;
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
                return false;
            }
            finally
            {
                parentList = null;
                newFieldInternalName = string.Empty;
                newField = null;
            }
        }

        #endregion

        #region Create SPField

        public static void CreateSingleLineColumn(SPList list, string columnName, bool required, bool showAdd, bool showEdit, bool showView)
        {
            string fieldInternalName = string.Empty;
            SPFieldText singleTextField = null;

            try
            {
                if (!list.Fields.ContainsField(columnName))
                {
                    fieldInternalName = list.Fields.Add(columnName, SPFieldType.Text, false);
                    singleTextField = list.Fields.GetFieldByInternalName(fieldInternalName) as SPFieldText;

                    if (singleTextField != null)
                    {
                        singleTextField.Required = required;
                        singleTextField.ShowInNewForm = showAdd;
                        singleTextField.ShowInEditForm = showEdit;
                        singleTextField.ShowInDisplayForm = showView;
                        singleTextField.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                fieldInternalName = null;
                singleTextField = null;
            }
        }

        public static void CreateMultiLineColumn(SPList list, string columnName, bool required, bool showAdd, bool showEdit, bool showView)
        {
            string fieldInternalName = string.Empty;
            SPFieldMultiLineText multiLineTextField = null;

            try
            {
                if (!list.Fields.ContainsField(columnName))
                {
                    fieldInternalName = list.Fields.Add(columnName, SPFieldType.Note, false);
                    multiLineTextField = list.Fields.GetFieldByInternalName(fieldInternalName) as SPFieldMultiLineText;

                    if (multiLineTextField != null)
                    {
                        multiLineTextField.Required = required;
                        multiLineTextField.RichText = true;
                        multiLineTextField.ShowInNewForm = showAdd;
                        multiLineTextField.ShowInEditForm = showEdit;
                        multiLineTextField.ShowInDisplayForm = showView;
                        multiLineTextField.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                fieldInternalName = null;
                multiLineTextField = null;
            }
        }

        // Added by Pradip G
        public static void CreateMultiLineColumnAsPlainText(SPList list, string columnName, bool required, bool showAdd, bool showEdit, bool showView)
        {
            string fieldInternalName = string.Empty;
            SPFieldMultiLineText multiLineTextField = null;

            try
            {
                if (!list.Fields.ContainsField(columnName))
                {
                    fieldInternalName = list.Fields.Add(columnName, SPFieldType.Note, false);
                    multiLineTextField = list.Fields.GetFieldByInternalName(fieldInternalName) as SPFieldMultiLineText;

                    if (multiLineTextField != null)
                    {
                        multiLineTextField.Required = required;
                        //multiLineTextField.RichText = true;
                        multiLineTextField.ShowInNewForm = showAdd;
                        multiLineTextField.ShowInEditForm = showEdit;
                        multiLineTextField.ShowInDisplayForm = showView;
                        multiLineTextField.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                fieldInternalName = null;
                multiLineTextField = null;
            }
        }
        public static void CreateDateTimeColumn(SPList list, string columnName, bool required, bool showAdd, bool showEdit, bool showView, SPDateTimeFieldFormatType displayType = SPDateTimeFieldFormatType.DateOnly)
        {
            string fieldInternalName = string.Empty;
            SPFieldDateTime dateTimeField = null;

            try
            {
                if (!list.Fields.ContainsField(columnName))
                {
                    fieldInternalName = list.Fields.Add(columnName, SPFieldType.DateTime, false);
                    dateTimeField = list.Fields.GetFieldByInternalName(fieldInternalName) as SPFieldDateTime;

                    if (dateTimeField != null)
                    {
                        dateTimeField.DisplayFormat = displayType;
                        dateTimeField.Required = required;
                        dateTimeField.ShowInNewForm = showAdd;
                        dateTimeField.ShowInEditForm = showEdit;
                        dateTimeField.ShowInDisplayForm = showView;
                        dateTimeField.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                fieldInternalName = null;
                dateTimeField = null;
            }
        }
        // Added by Pradip G - override function to set default column value
        public static void CreateDateTimeColumnForCurrentDate(SPList list, string columnName, bool required, bool showAdd, bool showEdit, bool showView, SPDateTimeFieldFormatType displayType = SPDateTimeFieldFormatType.DateOnly)
        {
            string fieldInternalName = string.Empty;
            SPFieldDateTime dateTimeField = null;

            try
            {
                if (!list.Fields.ContainsField(columnName))
                {
                    fieldInternalName = list.Fields.Add(columnName, SPFieldType.DateTime, false);
                    dateTimeField = list.Fields.GetFieldByInternalName(fieldInternalName) as SPFieldDateTime;

                    if (dateTimeField != null)
                    {
                        dateTimeField.DisplayFormat = displayType;
                        dateTimeField.Required = required;
                        dateTimeField.ShowInNewForm = showAdd;
                        dateTimeField.ShowInEditForm = showEdit;
                        dateTimeField.ShowInDisplayForm = showView;
                        // To set default value to Today.

                        DateTime today = Convert.ToDateTime(System.DateTime.Now.ToString("MM/dd/yyyy"));
                        dateTimeField.DefaultValue = today.ToString();// DateTime.Now.ToShortDateString();
                        dateTimeField.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                fieldInternalName = null;
                dateTimeField = null;
            }
        }

        public static void CreateNumberColumn(SPList list, string columnName, bool required, bool showAdd, bool showEdit, bool showView)
        {
            string fieldInternalName = string.Empty;
            SPFieldNumber numberField = null;

            try
            {
                if (!list.Fields.ContainsField(columnName))
                {
                    fieldInternalName = list.Fields.Add(columnName, SPFieldType.Number, false);
                    numberField = list.Fields.GetFieldByInternalName(fieldInternalName) as SPFieldNumber;

                    if (numberField != null)
                    {
                        numberField.DisplayFormat = SPNumberFormatTypes.NoDecimal;
                        numberField.Required = required;
                        numberField.ShowInNewForm = showAdd;
                        numberField.ShowInEditForm = showEdit;
                        numberField.ShowInDisplayForm = showView;
                        numberField.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                fieldInternalName = null;
                numberField = null;
            }
        }

        public static void CreateChoiceColumn(SPList list, string columnName, bool required, bool showAdd, bool showEdit, bool showView, string[] choices, bool isSiteColumn = true)
        {
            string fieldInternalName = string.Empty;
            SPFieldChoice choiceField = null;

            try
            {
                if (!list.Fields.ContainsField(columnName))
                {
                    if (list.ParentWeb.Site.RootWeb.Fields.ContainsField(columnName) && isSiteColumn)
                    {
                        choiceField = SharePointUtility.GetSiteColumn(list.ParentWeb.Site.RootWeb, columnName) as SPFieldChoice;
                        fieldInternalName = list.Fields.Add(choiceField);
                    }
                    else
                    {
                        fieldInternalName = list.Fields.Add(columnName, SPFieldType.Choice, false);
                    }

                    choiceField = list.Fields.GetFieldByInternalName(fieldInternalName) as SPFieldChoice;

                    if (choiceField != null)
                    {
                        choiceField.Required = required;
                        choiceField.ShowInNewForm = showAdd;
                        choiceField.ShowInEditForm = showEdit;
                        choiceField.ShowInDisplayForm = showView;
                        choiceField.Choices.AddRange(choices);
                        choiceField.FillInChoice = false;
                        choiceField.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                fieldInternalName = null;
                choiceField = null;
            }
        }

        public static void CreateCalculatedColumn(SPList list, string columnName, bool required, bool showAdd, bool showEdit, bool showView, string formula, SPFieldType fieldType = SPFieldType.Number)
        {
            string fieldInternalName = string.Empty;
            SPFieldCalculated calculatedField = null;

            try
            {
                if (!list.Fields.ContainsField(columnName))
                {
                    fieldInternalName = list.Fields.Add(columnName, SPFieldType.Calculated, false);
                    calculatedField = list.Fields.GetFieldByInternalName(fieldInternalName) as SPFieldCalculated;

                    if (calculatedField != null)
                    {
                        calculatedField.Formula = formula;
                        calculatedField.OutputType = fieldType;
                        calculatedField.Required = required;
                        calculatedField.ShowInNewForm = showAdd;
                        calculatedField.ShowInEditForm = showEdit;
                        calculatedField.ShowInDisplayForm = showView;
                        calculatedField.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                fieldInternalName = null;
                calculatedField = null;
            }
        }

        public static void CreateBooleanColumn(SPList list, string columnName, bool required, bool showAdd, bool showEdit, bool showView)
        {
            string fieldInternalName = string.Empty;
            SPFieldBoolean booleanField = null;

            try
            {
                if (!list.Fields.ContainsField(columnName))
                {
                    fieldInternalName = list.Fields.Add(columnName, SPFieldType.Boolean, false);
                    booleanField = list.Fields.GetFieldByInternalName(fieldInternalName) as SPFieldBoolean;

                    if (booleanField != null)
                    {
                        booleanField.DefaultValue = "0";
                        booleanField.Required = required;
                        booleanField.ShowInNewForm = showAdd;
                        booleanField.ShowInEditForm = showEdit;
                        booleanField.ShowInDisplayForm = showView;
                        if (columnName == Constants.COL_IS_FLAT_FILE)
                        {
                            booleanField.ReadOnlyField = true;
                        }
                        booleanField.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                fieldInternalName = null;
                booleanField = null;
            }
        }

        public static void CreateUserColumn(SPList list, string columnName, bool required, bool showAdd, bool showEdit, bool showView, bool allowMultipleValues = false)
        {
            string fieldInternalName = string.Empty;
            SPFieldUser numberField = null;

            try
            {
                if (!list.Fields.ContainsField(columnName))
                {
                    fieldInternalName = list.Fields.Add(columnName, SPFieldType.User, false);
                    numberField = list.Fields.GetFieldByInternalName(fieldInternalName) as SPFieldUser;

                    if (numberField != null)
                    {
                        numberField.Required = required;
                        numberField.ShowInNewForm = showAdd;
                        numberField.ShowInEditForm = showEdit;
                        numberField.ShowInDisplayForm = showView;
                        numberField.SelectionMode = SPFieldUserSelectionMode.PeopleOnly;
                        numberField.AllowMultipleValues = allowMultipleValues;
                        numberField.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                fieldInternalName = null;
                numberField = null;
            }
        }

        public static void CreateHyperlinkColumn(SPList list, string columnName, bool required, bool showAdd, bool showEdit, bool showView)
        {
            string fieldInternalName = string.Empty;
            SPFieldUrl hyperlinkField = null;

            try
            {
                if (!list.Fields.ContainsField(columnName))
                {
                    fieldInternalName = list.Fields.Add(columnName, SPFieldType.URL, false);
                    hyperlinkField = list.Fields.GetFieldByInternalName(fieldInternalName) as SPFieldUrl;

                    if (hyperlinkField != null)
                    {
                        hyperlinkField.DisplayFormat = SPUrlFieldFormatType.Hyperlink;
                        hyperlinkField.Required = required;
                        hyperlinkField.ShowInNewForm = showAdd;
                        hyperlinkField.ShowInEditForm = showEdit;
                        hyperlinkField.ShowInDisplayForm = showView;
                        hyperlinkField.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                fieldInternalName = null;
                hyperlinkField = null;
            }
        }

        public static void CreateLookupColumn(SPList list, string columnName, SPList lookupList, string lookupColumn, bool required)
        {
            string fieldInternalName = string.Empty;
            SPFieldLookup lookupField = null;
            try
            {
                fieldInternalName = list.Fields.AddLookup(columnName, lookupList.ID, required);
                lookupField = new SPFieldLookup(list.Fields, fieldInternalName);
                lookupField.LookupField = lookupColumn;
                lookupField.Title = columnName;
                lookupField.Update();
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
        }

        public static void AddSiteColumn(SPList list, string columnName, bool required = false)
        {
            string fieldInternalName = string.Empty;
            SPField siteColumnField = null;

            try
            {
                if (!list.Fields.ContainsField(columnName))
                {
                    siteColumnField = GetSiteColumn(list.ParentWeb.Site.RootWeb, columnName);

                    if (siteColumnField != null)
                    {
                        fieldInternalName = list.Fields.Add(siteColumnField);
                        if (required)
                        {
                            siteColumnField = list.Fields.GetFieldByInternalName(fieldInternalName);
                            siteColumnField.Required = true;
                            // siteColumnField.ReadOnlyField = true;
                            siteColumnField.Update();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                fieldInternalName = null;
            }
        }

        // Added by Pradip G to make Die No column as readonly
        public static void AddSiteColumnReadOnly(SPList list, string columnName, bool required = false)
        {
            string fieldInternalName = string.Empty;
            SPField siteColumnField = null;

            try
            {
                if (!list.Fields.ContainsField(columnName))
                {
                    siteColumnField = GetSiteColumn(list.ParentWeb.Site.RootWeb, columnName);

                    if (siteColumnField != null)
                    {
                        fieldInternalName = list.Fields.Add(siteColumnField);
                        if (required)
                        {
                            siteColumnField = list.Fields.GetFieldByInternalName(fieldInternalName);
                            siteColumnField.Required = true;
                            siteColumnField.ReadOnlyField = true;
                            siteColumnField.Update();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                fieldInternalName = null;
            }
        }

        /// <summary>
        /// This method adds site column to list
        /// </summary>
        /// <param name="list"></param>
        /// <param name="columnName"></param>
        /// <param name="required"></param>
        /// <param name="isuniqueValue"></param>
        public static void AddSiteColumn(SPList list, string columnName, bool required, bool isuniqueValue)
        {
            string fieldInternalName = string.Empty;
            SPField siteColumnField = null;

            try
            {
                if (!list.Fields.ContainsField(columnName))
                {
                    siteColumnField = GetSiteColumn(list.ParentWeb.Site.RootWeb, columnName);

                    if (siteColumnField != null)
                    {
                        fieldInternalName = list.Fields.Add(siteColumnField);
                        if (required)
                        {
                            siteColumnField = list.Fields.GetFieldByInternalName(fieldInternalName);
                            siteColumnField.Required = required;
                            siteColumnField.Indexed = true;
                            siteColumnField.EnforceUniqueValues = isuniqueValue;
                            siteColumnField.Update();
                        }
                    }
                }
                else
                {
                    siteColumnField = list.Fields[columnName];
                    siteColumnField.Required = required;
                    siteColumnField.Indexed = true;
                    siteColumnField.EnforceUniqueValues = isuniqueValue;
                    siteColumnField.Update();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                fieldInternalName = null;
            }
        }

        #endregion

        #region Add Webparts

        public static void SetWelcomePage(SPWeb web, string pageUrl)
        {
            SPFolder rootFolder = null;

            try
            {
                rootFolder = web.RootFolder;
                rootFolder.WelcomePage = pageUrl;
                rootFolder.Update();
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                rootFolder = null;
            }
        }

        public static void AddListViewWebPartToPage(SPWeb web, string listName, string viewName, string pageUrl, string zoneId, int zoneIndex)
        {
            SPList list = null;
            ListViewWebPart listViewWebPart = null;
            SPFile file = null;
            SPLimitedWebPartManager manager = null;

            try
            {
                file = web.GetFile(pageUrl);

                if (file.CheckOutType == SPFile.SPCheckOutType.None)
                {
                    list = web.Lists.TryGetList(listName);

                    if (list != null)
                    {
                        listViewWebPart = new ListViewWebPart();

                        listViewWebPart.ZoneID = zoneId;
                        listViewWebPart.ListName = list.ID.ToString("B").ToUpper();
                        listViewWebPart.ViewGuid = list.Views[viewName].ID.ToString("B").ToUpper();
                        listViewWebPart.ChromeType = PartChromeType.TitleOnly;

                        file.CheckOut();
                        manager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
                        manager.AddWebPart(listViewWebPart, zoneId, zoneIndex);
                        web.Update();

                        if (file != null)
                        {
                            if (file.CheckOutType != SPFile.SPCheckOutType.None)
                            {
                                file.CheckIn("Added web part");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                list = null;
                listViewWebPart = null;
                file = null;
                if (manager != null)
                    manager.Dispose();
            }
        }

        public static void AddCEWebPartToPage(SPWeb web, string webPartTitle, string scriptUrl, string pageUrl, string zoneId, int zoneIndex)
        {
            SPLimitedWebPartManager manager = null;
            SPFile file = null;

            try
            {
                file = web.GetFile(pageUrl);

                if (file.CheckOutType == SPFile.SPCheckOutType.None)
                {
                    //create new webpart object          
                    ContentEditorWebPart contentEditor = new ContentEditorWebPart();

                    //set properties of new webpart object    
                    contentEditor.ZoneID = zoneId;
                    contentEditor.Title = webPartTitle;
                    contentEditor.ChromeState = System.Web.UI.WebControls.WebParts.PartChromeState.Normal;
                    contentEditor.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.TitleOnly;
                    //Add content to CEWP                
                    contentEditor.ContentLink = scriptUrl;

                    file.CheckOut();
                    manager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);

                    if (manager != null)
                    {
                        //Add it to the zone
                        manager.AddWebPart(contentEditor, zoneId, zoneIndex);
                        web.Update();

                        if (file != null)
                        {
                            if (file.CheckOutType != SPFile.SPCheckOutType.None)
                            {
                                file.CheckIn("Added web part");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex);
            }
            finally
            {
                if (manager != null)
                    manager.Dispose();
                file = null;
            }
        }

        #endregion
    }
}

No comments:

Post a Comment