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();
    }
  }

Friday, April 15, 2011

SharePoint Read Only Databases and me as Developer

While working with SharePoint 2010 or SharePoint 2007, there are some scenarios when you want to take your content database of your web application to read only state.
For example when?
That’s a usual question one can ask , and answer is , when you / IT administrators are doing some migrations of your content database to some other system (like we do SharePoint 2007 upgrade to SharePoint 2010 by using database attach upgrade process) , or you / IT administrators are doing some maintenance of your SQL server
Practically doing this, you are making sure that end users are not adding contents to your site while you are working / migrating with content database
and good thing about SharePoint is , when you set your content database to read only state then SharePoint recognizes that and removes / hides all controls which cause any “Add” action to site
Well, you can do this in three ways :
a.    by using stsadm
b.    by using visual studio
c.     by using SQL Server Management Studio (easy for IT admins)
How to do this using stsadm?
Here is simple command which will put your site collection to read only mode and users wont be able to add new content
For read only site:
stsadm.exe –o setsitelock –url http://yoursite/ -lock readonly
for bringing site back to normal :
stsadm.exe –o setsitelock –url http://yoursite/ -lock none


How to do this using Visual Studio? (I love this J )     

static void Main(string[] args)
{
 try
 {
   using (SPSite site = new SPSite("http://yoursite/"))
   {
     site.AllowUnsafeUpdates = true;
     site.ReadOnly = true;
     Console.WriteLine(site.ReadOnly.ToString());
   }
 }
 catch (Exception ex)
 {
     Console.WriteLine(ex.Message);
 }

   Console.ReadLine();
 }


How can I do this as IT administrator?

·         Well that’s a pretty simple task if you having SQL Server Management Studio installed. Here are steps to do this
·         Open SQL Server Management Studio (for example for SQL Server 2008) and connect to the instance of database which is having your SharePoint content databases
·         Once you find your web application’s content database, right click on it and then select properties, now a small pop up window should open
·         in this new window , select Options from left menus and find option Database Read only and set that to true and you are done

Ok this is good so far but what’s in it for me as developer??
Yes interesting question, and obvious answer
while working with SharePoint and customizing / developing on top of SharePoint platform we usually create some web controls, web parts and so on , but one thing we should always consider in aspect of read only databases is , we should disable any “Add” functionality to site when database is in read only state
For example, I have a web part which adds some data to SharePoint list when clicked , and suppose site database is in read only state for some instance ,what will happen? Error isn’t it?
So to avoid this, we should always check database status of site like this

if (!site.WebApplication.ContentDatabases[0].IsReadOnly)
{
  if (!site.ReadOnly)
  {
    //Enable Add/Edit functionality
  }
}
else
{
    //Disable Add/Edit functionality
}

SharePoint 2010 List Events Enhancements

Have you ever worked with SharePoint 2007 List Item Events? If yes then you would have probably noticed that when we attach any event receiver with list then it gets attached at the template level
what I mean is suppose I have created an event receiver for say Calculations List which is basically a custom list (template id = 100) , so in SharePoint 2007 all events will be called for all lists which are created by using  custom list template id = 100
and workaround was that , we were taking help of code to avoid this in SP 2007 , like we can check the name of list every time event receiver gets called and make execution only for desired list
Sample Code:
public override void ItemAdded(SPItemEventProperties properties)
{
  if (properties.ListTitle.Equals("YourList"))
  {
     //here you go          
  }
}
Now to avoid this in SharePoint 2010, framework comes with some enhancements done in Event Receivers section
Now we can associate particular event receiver at Site / Web / List levels and so this is more granular now
 Basically there are three more attributes are added for <Receivers> element and those are
Scope: we can define the scope of Event Receiver to SiteCollection (Site) or Web level
RootWebOnly: event receiver will be attached to all lists under root web created using particular template
ListUrl: we can specify particular list on which Event Receiver will be active (/Lists/MyList/)
Example:
<Receivers ListTemplateId="100" ListUrl="/Lists/CustomList/">
    <Receiver>
      <Name></Name>
      <Type></Type>
      <Assembly></Assembly>
      <Class></Class>
      <SequenceNumber></SequenceNumber>
    </Receiver>
 </Receivers>

Tuesday, March 29, 2011

Export SharePoint List Programmatically

If you try to search on internet on this topic, there are many posts available and each one uses different technique to achieve this
last week, one of my colleague asked me how they can export the SharePoint list content (they were accessing sites remotely) , well answer was quite simple , click on actions tab on list and then select export to spread sheet and save file , but what really puzzled me is , what are other OOB ways available by SharePoint to achieve this other than this option?
I tried searching for this for few hours and ended up with writing a sample console application which exports the list’s default view and write list contents to spreadsheet
Major disadvantage of this is, you cannot run this application when you are not directly working with SharePoint Server, because we are using Server side object model to achieve this
I am keen to see this in SharePoint 2010 environment because we can use Client Object Model to achieve this
But for now here is code I created
I know this code is little heavy (due to those foreach loops) but I thought this is ok as this won’t be running continuously on server


class Program
{

  private static DataTable dataTable;
  private static SPList list;

  static void Main(string[] args)
  {
   try
   {
    Console.WriteLine("Site Url: ");
    string _siteUrl = Console.ReadLine();
    if (!string.IsNullOrEmpty(_siteUrl))
    {
     SPSecurity.RunWithElevatedPrivileges(delegate()
     {
       using (SPSite site = new SPSite(_siteUrl))
       {
         if (site != null)
         {
          SPWeb web = site.RootWeb;

          if (web != null)
          {

            #region Export List

            Console.WriteLine("List Name:");
            string _listName = Console.ReadLine();
             if (!string.IsNullOrEmpty(_listName))
             {
               list = web.Lists[_listName];

               if (list != null)
               {
                 dataTable = new DataTable();

                 //Adds Columns to SpreadSheet
                  InitializeExcel(list, dataTable);

                  string _schemaXML = list.DefaultView.ViewFields.SchemaXml;

                  if (list.Items != null && list.ItemCount > 0)
                  {
                   foreach (SPListItem _item in list.Items)
                   {
                     DataRow dr = dataTable.NewRow();
                     foreach (DataColumn _column in dataTable.Columns)
                     {
if (dataTable.Columns[_column.ColumnName] != null   && _item[_column.ColumnName] != null)
                      {
dr[_column.ColumnName] = _item[_column.ColumnName].ToString();
                      }
                     }
                     dataTable.Rows.Add(dr);

                    }
                   }

                 }
              }

System.Web.UI.WebControls.DataGrid grid = new System.Web.UI.WebControls.DataGrid();

          grid.HeaderStyle.Font.Bold = true;
          grid.DataSource = dataTable;
          grid.DataBind();

          using (StreamWriter streamWriter = new StreamWriter("C:\\" + list.Title + ".xls", false, Encoding.UTF8))
          {
           using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(streamWriter))
           {
             grid.RenderControl(htmlTextWriter);
           }
          }

             Console.WriteLine("File Created");

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

      Console.ReadLine();
    }

   public static void InitializeExcel(SPList list, DataTable _datatable)
   {
    if (list != null)
    {
     string _schemaXML = list.DefaultView.ViewFields.SchemaXml;
     if (list.Items != null && list.ItemCount > 0)
     {
      foreach (SPListItem _item in list.Items)
      {
       foreach (SPField _itemField in _item.Fields)
       {
        if (_schemaXML.Contains(_itemField.InternalName))
        {
          if (_item[_itemField.InternalName] != null)
          {
           if (!_datatable.Columns.Contains(_itemField.InternalName))
           {
             _datatable.Columns.Add(new DataColumn(_itemField.StaticName, Type.GetType("System.String")));
           }
          }
         }
        }
       }
      }
     }
    }
   }