Pages

Wednesday, May 25, 2011

Resize Image before uploading to SharePoint Image Library programmatically

There are some times when we might want to check /restrict the height and width of Image before uploading to SharePoint Images Library
to address this situation , I have tried creating a event receiver which checks the width and height of image which we are uploading from file system , and If found big then resize to 200x200
public override void ItemAdded(SPItemEventProperties properties)
{
    int _imageWidth = 0;
    int _imageHeight = 0;

    if (properties.ListTitle.ToLower().Equals("images"))
    {
      try
      {
        string _width = properties.ListItem.File.Properties["vti_lastwidth"].ToString();
        string _height = properties.ListItem.File.Properties["vti_lastheight"].ToString();

        if (Int32.TryParse(_width, out _imageWidth) && Int32.TryParse(_height, out _imageHeight))
        {
          if (_imageWidth > 200 && _imageHeight > 200)
          {
            SPFile _imageFile = properties.ListItem.File;

            MemoryStream _inputStream = new MemoryStream(_imageFile.OpenBinary(), true);
            MemoryStream _outputStream = new MemoryStream();

            Image _resizedImage = Image.FromStream(_inputStream).GetThumbnailImage(200, 200, null, IntPtr.Zero);
            _resizedImage.Save(_outputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

             _imageFile.SaveBinary(_outputStream, false);
             _imageFile.CheckOut();
             _imageFile.Update();
             _imageFile.CheckIn("Image Resized");
             if (properties.ListItem.ParentList.EnableMinorVersions)
             {
               properties.ListItem.File.Publish("ImagePublished");
             }
             if (properties.ListItem.ParentList.EnableModeration)
             {
               properties.ListItem.File.Approve("ImageApproved");
             }

           }
         }
       }
       catch (Exception ex)
       {
         throw new SPException("Error: " + ex.StackTrace);
       }
      }
   }

Note : use System.Drawing Namespace for using class Image

Reference : MSDN Forums Post

Monday, May 23, 2011

Adding PublishingPageImage programmatically – SharePoint Publishing Pages

Here is simple console application, which adds /sets the Image for SharePoint Publishing Page
Well, some basics before to dive in to the code,
When you create any Publishing Page in SharePoint then by default it gets created from Publishing Page content type, and so by default it has some fields to store the associated metadata
If you want to see names of all fields associated with default publishing page content type then this is how simply you can do
Go to 12 hive feature folder, search feature with name PublishingResources, open file with name PublishingColumns.xml (you can find more files in this feature such as PublishingContentTypes.xml , If you are interested to know then you can have look at these files too)
class Program
{
 static void Main(string[] args)
 {
  try
  {
    using (SPSite site = new SPSite("http://yoursite"))
    {
      using (SPWeb web = site.RootWeb)
      {
        if (PublishingWeb.IsPublishingWeb(web))
        {
          PublishingWeb _pweb = PublishingWeb.GetPublishingWeb(web);
          if (_pweb != null)
          {
            if (_pweb.DefaultPage.Level != SPFileLevel.Checkout)
            {
             if (_pweb.DefaultPage.Item[FieldId.PublishingPageImage] as  ImageFieldValue == null)
             {
               _pweb.DefaultPage.CheckOut();
               ImageFieldValue _field = new ImageFieldValue();
               _field.ImageUrl = "/SiteCollectionImages/home.gif";

                                          _pweb.DefaultPage.Item[FieldId.PublishingPageImage] = _field;
            _pweb.DefaultPage.Item.Update();
            _pweb.DefaultPage.CheckIn("Added Image");
            if (_pweb.PagesList.EnableMinorVersions)
            {
              _pweb.DefaultPage.Publish("Published");
             }
           if (_pweb.PagesList.EnableModeration)
           {
             _pweb.DefaultPage.Approve("Approved!!");
           }

              Console.WriteLine("Image Added");
          }
                                   
         }
        }
       }
      }
     }
    }
    catch (Exception ex)
    {
      Console.WriteLine("Error :" + ex.Message);
    }

       Console.ReadLine();
    }
  }