[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,

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s