locked
how to create a webpart in visual studio 2010 and retrieve items from a document library? RRS feed

  • Question

  • Sharepoint 2010 - Visual studio 2010

    I am trying to create a webpart that displays all items in a document library, with a column to change the order of rows.

    I'm not sure how to retrieve this list and get all the items.

    Anyone know any tutorials to help?


    • Edited by ryands Thursday, July 26, 2012 4:54 PM
    Thursday, July 26, 2012 2:50 PM

Answers

  • Hi,

    You can create visual webpart. Please follow the below step:

    1. Create visual webpart--follow this MSDN link

    http://msdn.microsoft.com/en-us/library/ff597539.aspx

    2. Now click on visualwebpart1usercontrol.ascx and paste below code:

    <asp:GridView ID="grdDocuments" runat="server" AutoGenerateColumns="false" >
    <Columns>
    <asp:HyperLinkField DataTextField="Title" DataNavigateUrlFields="URL" />
    </Columns>
    </asp:GridView>

    3. Now click on isualwebpart1usercontrol.ascx.cs file and paste below code:

    void BindDocuments()
            {
                using(SPWeb mySite = SPContext.Current.Web)
               {
                SPList myList = mySite.Lists["MyDocsLibName"];//change library name
                SPListItemCollection items = myList.Items;
                //Here we will make a datatable and we will put our document library data to the data table
                DataTable table;
                table = new DataTable();
                table.Columns.Add("Title", typeof(string));
                table.Columns.Add("URL", typeof(string));
    
                // Create rows for each splistitem
                DataRow row;
                foreach (SPListItem result in items)
                {
                    row = table.Rows.Add();
                    row["Title"] = result.Name;
                 
                    row["URL"] = "Your site url" + result.Url; //here need to append the site url to the item url
                }
                grdDocuments.DataSource = table.DefaultView;
                grdDocuments.DataBind();
            }
    }

    4. Now need to deploy the solution so go to build menu-->Build Solution and click deploy solution.

    5. Now go to your site and edit your site page. Add your webpart from custom group.

    Check and revert for any issue





    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    • Proposed as answer by Naveen_D Friday, July 27, 2012 9:38 AM
    • Marked as answer by Lhan Han Thursday, August 2, 2012 7:10 AM
    Friday, July 27, 2012 7:18 AM

All replies

  • Thursday, July 26, 2012 2:59 PM
  • You can create a custom web part, leverage SPGridView control and bind the data source (list of documents). The SPGridView control would help you to implementing sorting also ('a column to change the order of rows').

    http://sharepointmalaya.blogspot.in/2009/07/adding-drop-down-menu-items-to.html

    http://msdn.microsoft.com/en-us/library/bb466219(v=office.12).aspx

    http://blogs.msdn.com/b/powlo/archive/2007/02/25/displaying-custom-data-through-sharepoint-lists-using-spgridview-and-spmenufield.aspx

    Hope this helps !


    Sundar Narasiman

    Thursday, July 26, 2012 3:01 PM
  • hi ryands

    from your prev post http://social.msdn.microsoft.com/Forums/en/sharepoint2010programming/thread/e429740a-0b30-4ef2-a909-7b151e000ef8 and current post.

    you most learn Using Of Listview Web part carefully.  u can customize list view web part and get answer for both questions.


    yaşamak bir eylemdir

    Thursday, July 26, 2012 3:04 PM
  • I saw this post from your link

    http://www.devexpertise.com/2009/02/25/integrating-a-custom-aspnet-application-into-sharepoint-part-2/

    But I don't understand how to load a document library into this webpart.

    Thursday, July 26, 2012 3:07 PM
  • Hi

    That means you want some kind of grid in your webpart. SPGridView eg. supports sorting

    SPGridView in general

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.spgridview.aspx

    A blog that describes how to do sorting (NB! 2007-thread) in SPGridView

    http://blogs.msdn.com/b/powlo/archive/2007/03/23/adding-paging-to-spgridview-when-using-custom-data-sources.aspx 

    To quickly display the library contents into a grid you can simply add the following code in eg. CreateChildControls()


    protected override void CreateChildControls() 
    { 
       SPWeb web = SPContext.Current.Web; 
       SPList spList = web.Lists[<your_library_name>]; 
       SPDataSource listSource = new SPDataSource(); 
       listSource.List = spList;  
       SPGridView spGrid = new SPGridView();
       spGridrid.DataSource = listSource;  
       spGrid.DataBind();  
       Controls.Add(spGrid);
    } 


    Kind Regards Bjoern
    Blog



    Thursday, July 26, 2012 3:16 PM
  • I'm pretty new at sharepoint api programming and have no idea where to begin. All these tutorials, I am not understanding them at all. Can someone show me some quick steps on where to begin, something like:

    -open VS2010,

    -create new sharepoint 2010 empty project

    -insert web part

    -add this code to connect to the sharepoint site

    -add this code to load this document library

    -add this code to create the gridview

    -add this code to loop through all the items and insert it into the webpart

    -add this code to add buttons to move the item row up/down (which changes a field value of the item in the original list, and that document library is sorted by that order value)

    If I could understand how to do this, that would be amazing!

    I really need to figure out how to do this.

    Thursday, July 26, 2012 3:22 PM
  • Hi

    That means you want some kind of grid in your webpart. SPGridView eg. supports sorting

    SPGridView in general

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.spgridview.aspx

    A blog that describes how to do sorting (NB! 2007-thread) in SPGridView

    http://blogs.msdn.com/b/powlo/archive/2007/03/23/adding-paging-to-spgridview-when-using-custom-data-sources.aspx 

    To quickly display the library contents into a grid you can simply add the following code in eg. CreateChildControls()


    protected override void CreateChildControls() 
    { 
       SPWeb web = SPContext.Current.Web; 
       SPList spList = web.Lists[<your_library_name>]; 
       SPDataSource listSource = new SPDataSource(); 
       listSource.List = spList;  
       SPGridView spGrid = new SPGridView();
       spGridrid.DataSource = listSource;  
       spGrid.DataBind();  
       Controls.Add(spGrid);
    } 


    Kind Regards Bjoern
    Blog



    I tried this (below) but it says "spGridrid" doesn't exist in the current context.

    using System;
    using System.ComponentModel;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using System.Data;
    
    namespace ItemOrdering.DataGridWebPart
    {
        [ToolboxItemAttribute(false)]
        public class DataGridWebPart : WebPart
        {
    
            private DataGrid grid;
            protected string data_path;
    
            [Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
                WebDisplayName("Path to List or Library"),
                WebDescription("Location of the data source to load")]
            public string DataFilePath
            {
                get
                {
                    return data_path;
                }
                set
                {
                    data_path = value;
                }
            }
    
    
            protected override void CreateChildControls() 
            { 
               SPWeb web = SPContext.Current.Web;
               SPList spList = web.Lists[data_path]; 
               SPDataSource listSource = new SPDataSource(); 
               listSource.List = spList;  
               SPGridView spGrid = new SPGridView();
               spGridrid.DataSource = listSource;  
               spGrid.DataBind();  
               Controls.Add(spGrid);
            } 
        }
    }
    

    Thursday, July 26, 2012 4:34 PM
  • Here are a couple of links that walks you through webpart development from the startup:

    http://msdn.microsoft.com/en-us/library/gg413295.aspx

    http://msdn.microsoft.com/en-us/library/ff769953

    You have a typo in your code.  If you change

    spGridrid.DataSource = listSource; 

    to

    spGrid.DataSource = listSource;

    it should work


    Kind Regards Bjoern
    Blog



    Thursday, July 26, 2012 6:05 PM
  • Hi,

    You can create visual webpart. Please follow the below step:

    1. Create visual webpart--follow this MSDN link

    http://msdn.microsoft.com/en-us/library/ff597539.aspx

    2. Now click on visualwebpart1usercontrol.ascx and paste below code:

    <asp:GridView ID="grdDocuments" runat="server" AutoGenerateColumns="false" >
    <Columns>
    <asp:HyperLinkField DataTextField="Title" DataNavigateUrlFields="URL" />
    </Columns>
    </asp:GridView>

    3. Now click on isualwebpart1usercontrol.ascx.cs file and paste below code:

    void BindDocuments()
            {
                using(SPWeb mySite = SPContext.Current.Web)
               {
                SPList myList = mySite.Lists["MyDocsLibName"];//change library name
                SPListItemCollection items = myList.Items;
                //Here we will make a datatable and we will put our document library data to the data table
                DataTable table;
                table = new DataTable();
                table.Columns.Add("Title", typeof(string));
                table.Columns.Add("URL", typeof(string));
    
                // Create rows for each splistitem
                DataRow row;
                foreach (SPListItem result in items)
                {
                    row = table.Rows.Add();
                    row["Title"] = result.Name;
                 
                    row["URL"] = "Your site url" + result.Url; //here need to append the site url to the item url
                }
                grdDocuments.DataSource = table.DefaultView;
                grdDocuments.DataBind();
            }
    }

    4. Now need to deploy the solution so go to build menu-->Build Solution and click deploy solution.

    5. Now go to your site and edit your site page. Add your webpart from custom group.

    Check and revert for any issue





    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    • Proposed as answer by Naveen_D Friday, July 27, 2012 9:38 AM
    • Marked as answer by Lhan Han Thursday, August 2, 2012 7:10 AM
    Friday, July 27, 2012 7:18 AM