HomeDevelopment (Old) Publish and approve file programmatically sharepoint 2010

[SharePoint 2010 – Developpement] Publish and approve file programmatically sharepoint 2010

I created a module which deploy a file (itemStyle for a ContentQueryWepPart), and i wanted to publish and approve it directly programmatically.
In the feature of your module :

  • Create an event Receiver
  • Add this code :
   1: public override void FeatureActivated(SPFeatureReceiverProperties properties)
   2: {
   3:     publishItemStyle(properties);
   4: }
   5:  
   6: private void publishItemStyle(SPFeatureReceiverProperties properties)
   7: {
   8:     try
   9:     {
  10:         SPWeb web = (SPWeb)properties.Feature.Parent;
  11:         SPFile file = web.GetFile("Style Library/Xsl Style Sheets/ItemStyle.xsl");
  12:  
  13:         if (file.Level == SPFileLevel.Checkout)
  14:             file.CheckIn("", SPCheckinType.MajorCheckIn);
  15:         if (file.Level == SPFileLevel.Draft)
  16:         {
  17:             if (file.DocumentLibrary.EnableModeration) file.Approve("");
  18:             else file.Publish("");
  19:         }
  20:         file.Update();
  21:     }
  22:     catch (Exception e)
  23:     {
  24:     }
  25: }

Hope this help,

- Advertisement -

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

CONNECT FOR MORE CONTENT

DO NOT MISS THOSE ARTICLES

Recent Comments