You are here: Home » how to publish the entire document library

how to publish the entire document library

Posted by mingssn
No Comments »

Sharepoint does not provide "Publish All" feature. If you select Site Action -> Manage Content and Structure you will soon find this page is kind of buggy. You could select multiple files to publish all but it is page based, which means it only publish the files you selected in the current page not the entire document library. Also, if the work flow is enabled for this document library, "Publish All" item will greyed out.

so….write you own code to do this.

        protected bool PublishPpmContent(SPWeb web)
        {
            try
            {
                SPDocumentLibrary docLibrary = GetLibraryByName("myDocLibName");
                //publish files
                SPListItemCollection docLibItems = docLibrary.Items;
                foreach (SPListItem docLibItem in docLibItems)
                {
                    if (docLibItem["Approval Status"].ToString() != GlobalConst.Approved && docLibItem["Approval Status"].ToString() != GlobalConst.Rejected)//not approved, not rejected
                    {
                        SPFile thisFile = getFileByName(web, docLibItem.Url);
                        if (thisFile != null && thisFile.CheckedOutBy == null)
                        {
                            thisFile.Publish(string.Format("Published by {0} at {1}", web.CurrentUser.Name, DateTime.Now.ToLongDateString()));
                            thisFile.Approve(string.Format("Approved by {0} at {1}", web.CurrentUser.Name, DateTime.Now.ToLongDateString()));
                        }
                    }
                }
                //approve all folders
                SPListItemCollection docFolderCollection = docLibrary.Folders;
                foreach (SPListItem docFolder in docFolderCollection)
                {
                    if (docFolder["Approval Status"].ToString() != GlobalConst.Approved &&
                        docFolder["Approval Status"].ToString() != GlobalConst.Rejected)
                    {
                        SPFolder thisFolder = getFolderByName(web, docFolder.Url);
                        if (thisFolder.Exists)
                        {
                            thisFolder.Item.ModerationInformation.Status = SPModerationStatusType.Approved;
                            thisFolder.Item.Update();
                        }
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }

        }

        private SPDocumentLibrary GetLibraryByName(string libraryName)
        {
            SPSite mySite = new SPSite("Your site url");
            foreach (SPList list in mySite.OpenWeb().Lists)
            {
                if (list.BaseType == SPBaseType.DocumentLibrary && list.Title == libraryName)
                    return (SPDocumentLibrary)list;
            }
            return null;
        }
         public SPFolder getFolderByName(SPWeb web, string folderName)
        {
            SPListCollection lists = web.Lists;
            foreach (SPList list in lists)
            {
                if (list.BaseType == SPBaseType.DocumentLibrary)
                {
                    SPDocumentLibrary docLibrary = (SPDocumentLibrary)list;
                    if (!docLibrary.IsCatalog &&
                        list.BaseTemplate != SPListTemplateType.XMLForm &&
                        docLibrary.Title == GlobalConst.PpmSourceFileLibraryName)
                    {
                        SPListItemCollection docLibFolders = docLibrary.Folders;
                        foreach (SPListItem docLibItem in docLibFolders)
                        {
                            if (docLibItem.Folder.ServerRelativeUrl == "/" + folderName)
                                return docLibItem.Folder;
                        }
                    }
                }
            }
            return null;
        }

 

Note: 

1. Document library may contains folder, you need approve files and folders when do the entire publish. 
2. GlobalConst has a property Approved = "0", Rejected = "1", Pending = "2", Draft = "3"
3. Only work with VSS 3.0 not for Sharepoint 2003.

 

Your email is never shared.
Required fields are marked *




Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>