Thursday, February 23, 2012

Update Choice Column value in Item Updating Event

Leave a Comment
One of my friend was facing issue to update value of choice column in Item Updating event.  Founded solution that if value of list item is needed to be change in item updating event then set after properties of property  and then fire base item updating event.

Reference Code  :

       public override void ItemUpdating(SPItemEventProperties properties)
       {
           properties.AfterProperties["Status"] = "Item1";
           base.ItemUpdating(properties);
       }
Read More...

Show list of files from Document Library in webpart as Sandboxed Solutions

1 comment
In this example, we will learn how to show  file name,  server relative url , extension and document icon type of all files present in sharepoint document library in Webpart as a Sandboxed solution.

Step 1. First of all create a SharePoint Empty Project through Visual Studio deployed as sandboxed solution.

Step 2. Then add Web Part to empty project.

Step 3.  Declare global variable under Web part class

             readonly DataTable _dt = new DataTable();       

Step  4 .  Create method to get all the files from the root folder of document library  to put information inside data table. 

        "MyDocuments" is the name of share point document library.
         
         public void PopulateDataTable()
        {
            var documentLibrary = SPContext.Current.Web.Lists["MyDocuments"] as SPDocumentLibrary;

            if (documentLibrary != null && documentLibrary.ItemCount > 0)
            {
                _dt.Columns.Add(new DataColumn("Title", typeof(string)));
                _dt.Columns.Add(new DataColumn("Url", typeof(string)));
                _dt.Columns.Add(new DataColumn("Extension", typeof(string)));
                _dt.Columns.Add(new DataColumn("ImageUrl", typeof (string)));

                foreach (SPFile file in documentLibrary.RootFolder.Files)
                {
                    DataRow dataRow = _dt.NewRow();
                    dataRow[0] = file.Name;
                    dataRow[1] = file.ServerRelativeUrl;
                    dataRow[2] = file.Name.Substring(file.Name.LastIndexOf(".") + 1);
                    dataRow[3] = "_layouts/images/lg_IC" + file.Name.Substring(file.Name.LastIndexOf(".") + 1).ToUpper() + ".png" ;
                    _dt.Rows.Add(dataRow);
                }

            }  

Many files types icons are already available in 14/Images Folder with type name lg_ICExtension.png or gif.
For example if file is of type docx then icon will be of name lg_ICDOCX.PNG.

If images of certain file types are not present at that location then , then he/she can create and copy icon to 14 hive images folder, so that gridview will able to show file icons.   

Step 6. Create GridView infrastructure to show information.

            var gridView = new GridView {ID = "MyGridView", AutoGenerateColumns = false};

            var imageField = new ImageField { HeaderText = "", DataImageUrlField = "ImageUrl"};
            gridView.Columns.Add(imageField);

            var fileName = new BoundField {HeaderText = "File Name", DataField = "Title"};
            gridView.Columns.Add(fileName);

            var url = new BoundField {DataField = "Url", HeaderText = "Url"};
            gridView.Columns.Add(url);

            var extension = new BoundField {DataField = "Extension", HeaderText = "Extension"};
            gridView.Columns.Add(extension);

            gridView.DataSource = _dt;
            gridView.DataBind();
            Controls.Add(gridView);


Complete Code

[ToolboxItemAttribute(false)]
    public class MyGridView : WebPart
    {
        readonly DataTable _dt = new DataTable();

        protected override void CreateChildControls()
        {
            PopulateDataTable();

            var gridView = new GridView {ID = "MyGridView", AutoGenerateColumns = false};

            var imageField = new ImageField { HeaderText = "", DataImageUrlField = "ImageUrl"};
            gridView.Columns.Add(imageField);

            var fileName = new BoundField {HeaderText = "File Name", DataField = "Title"};
            gridView.Columns.Add(fileName);

            var url = new BoundField {DataField = "Url", HeaderText = "Url"};
            gridView.Columns.Add(url);

            var extension = new BoundField {DataField = "Extension", HeaderText = "Extension"};
            gridView.Columns.Add(extension);

            gridView.DataSource = _dt;
            gridView.DataBind();
            Controls.Add(gridView);
        }

        public void PopulateDataTable()
        {
            var documentLibrary = SPContext.Current.Web.Lists["MyDocuments"] as SPDocumentLibrary;

            if (documentLibrary != null && documentLibrary.ItemCount > 0)
            {
                _dt.Columns.Add(new DataColumn("Title", typeof(string)));
                _dt.Columns.Add(new DataColumn("Url", typeof(string)));
                _dt.Columns.Add(new DataColumn("Extension", typeof(string)));
                _dt.Columns.Add(new DataColumn("ImageUrl", typeof (string)));

                foreach (SPFile file in documentLibrary.RootFolder.Files)
                {
                    DataRow dataRow = _dt.NewRow();
                    dataRow[0] = file.Name;
                    dataRow[1] = file.ServerRelativeUrl;
                    dataRow[2] = file.Name.Substring(file.Name.LastIndexOf(".") + 1);
                    dataRow[3] = "_layouts/images/lg_IC" + file.Name.Substring(file.Name.LastIndexOf(".") + 1).ToUpper() + ".png" ;
                    _dt.Rows.Add(dataRow);
                }

            }
        }
    }






Read More...