SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

how to publish the entire document library

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.

 


Posted 12-20-2007 3:30 PM by mingssn

Comments

Tom wrote re: how to publish the entire document library
on 05-28-2008 2:18 AM

Where are you supposed to put this code?

Jk wrote re: how to publish the entire document library
on 09-10-2008 3:16 AM

As tom said, where to put the code, we are in need of this too..

Mike Knowles wrote re: how to publish the entire document library
on 01-07-2009 1:28 PM

Just what I needed, thanks.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.