Thursday, 17 January 2013

Code to create column in the existing document library



public static void AddCustomDocumentLibraryColumn(string documentLibraryName, string columnName,string columnDisplayName,string columnDescription, SPFieldType columnDataType)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPDocumentLibrary docLibrary = null;
                            if (web.TryGetDocumentLibrary(documentLibraryName, out docLibrary))
                            {

                                if (!docLibrary.Fields.ContainsField(columnName))
                                {
                                    bool allowUnsafeUpdateState = web.AllowUnsafeUpdates;
                                    web.AllowUnsafeUpdates = true;
                                    SPView defaultView = docLibrary.DefaultView;
                                  
                                    SPField libField = docLibrary.Fields.GetField( docLibrary.Fields.Add(columnName, columnDataType, true));
                                    libField.Description = columnDescription;
                                    libField.Title = columnDisplayName;
                                    libField.Required = true;
                                    libField.Update();
                                    defaultView.ViewFields.Add(libField);
                                    defaultView.Update();
                                    docLibrary.Update();
                                    web.Update();
                                    web.AllowUnsafeUpdates = allowUnsafeUpdateState;
                                }
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {

            }
        }


TryGetDocumentLibrary is an extended function of the Web.

public static bool TryGetDocumentLibrary(this SPWeb web, string documentLibraryName,
                                                                       out SPDocumentLibrary documentLibrary)
        {

            bool isDocumentLibraryExist = false;
            web.AllowUnsafeUpdates = true;

            if (true == web.Properties.ContainsKey(documentLibraryName))
            {

                try
                {

                    documentLibrary = web.Lists[new Guid(web.Properties[documentLibraryName])]
                                                                                    as SPDocumentLibrary;
                    isDocumentLibraryExist = true;

                }
                catch
                {

                    web.Properties[documentLibraryName] = null;
                    web.Properties.Update();

                    TryGetDocumentLibrary(web, documentLibraryName, out documentLibrary);

                }

            }
            else
            {

                documentLibrary = (from SPList list in web.Lists
                                   where list.RootFolder.Name.Equals(documentLibraryName,
                                                            StringComparison.InvariantCulture)
                                   select list).FirstOrDefault() as SPDocumentLibrary;

                if (documentLibrary != null)
                {

                    web.Properties.Add(documentLibraryName, documentLibrary.ID.ToString("B"));
                    web.Properties.Update();
                    isDocumentLibraryExist = true;
                }

            }

            web.AllowUnsafeUpdates = false;

            return isDocumentLibraryExist;

        }