Question : Programatically Publishing the  Document Items

Hi,

I have to programitacally publish  the newly added items to the Document List.
Following is the code snippet I am trying...

SPFile docFile= site.Files.Add(libraryPath + DocName, fs, true);

docFile.Publish("Published Programatically");


But throws an exception saying "Microsoft.SharePoint.SPException: You can only publish, unpublish  documents in a minor version enabled list"

Any ideas what am I doing wrong?



Thanks in advance.


Answer : Programatically Publishing the  Document Items

The call to the Publish method is raising an exception, because minor versions aren't enabled on your list, added files are 'published' by default.

You could get rid of the publish, or you could add some validation before publishing and approving like the attached snippet.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
SPList list = web.GetList(strUrl);
                foreach (SPListItem item in list.Items)
                {
                    SPFile sourceFile = item.File;
 
                    if ((sourceFile.Level == SPFileLevel.Draft) && (sourceFile.CheckOutStatus == SPFile.SPCheckOutStatus.None))
                    {
                        sourceFile.Publish("File Automatically Bulk Published");
 
                        try
                        {
                            if
(sourceFile.Item.ModerationInformation.Status ==
SPModerationStatusType.Pending)
                            {
                                sourceFile.Approve("File Automatically Approved During Bulk Publish");
                            }
                        }
                        catch (System.Exception)
                        {
                            //Suppress
                        }
                        sourceFile.Update();
                    }
                }
Random Solutions  
 
programming4us programming4us