0

I'm new on SharePoint development (just 3 days) and next monday I start a project.

I try to understand its structure and its coding bases and object model.

I just created a simple webpart to figure out how it works.

I would like to know if to get it disponible to activate/desactivat in my sharepoint web site I need to create a feature or if I just need to deploy it without a feature.

Well can someone too tell me the diference between WebPart and Visual WebPart?

Thank you.

3 Answers 3

2

anima nearly had it!

webpart: has a .cs and you programmatically add the controls within the creatchildcontrols

visual webpart: its also has the .cs but rather it inherits the visual aspect (UI) from a ascx page that also has its own .cs

visual webparts are to seperate the design aspect from the codeing aspect, You could go ahead and change the ascx without having to rebuild the project... thats the principle behind the visual webpart. easier to maintain for branding ect without having to open the project within vs and rebuilding

either way is up to you! i dont quite get what you mean ;) either way in sharepoint 2010 or 2013 using visual studios 2010 you can add to the feature an event handler from there sharepoint auto adds the feature adding and removing!

to add a feature, within the project on the right hand side look for feature, within feature look for feature.feature, right click and click add event reciver!

its that simple!

a step by step guide

http://msdn.microsoft.com/en-us/library/vstudio/ee231604(v=vs.100).aspx

to add a list in a visual webpart you need to use SPGridview or Gridview its up to you!

open your ascx page and add the following code:

<div style=" position:relative"> 
    <asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" Width="95%" GridLines="None" AllowPaging="true">  
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="Request ref" />
            <asp:BoundField DataField="Title" HeaderText="Title" />
            <asp:BoundField DataField="Age" HeaderText="Business area" />   
            <asp:BoundField DataField="Status" HeaderText="Status" />      
        </Columns>
    </asp:GridView>

the above is getting 4 columns of data, ID, title, age and status from my list:

to bind the list to the gridview to get the data that is simple! now goto your ascx.cs, double click. within the onload event add this:

        using (SPSite site = new SPSite(SPContext.Current.Site.Url))
        {
            using (SPWeb web = site.OpenWeb("mySubSite"))
            {
                SPList list = web.Lists["USER INPUT"];
                SPListItemCollection items = list.Items;
                grid.DataSource = items.GetDataTable();
                grid.DataBind();

            }
        }

the above is calling the site from the current url, its then access the subsite mySubSite to give you an understanding:

        site              subsite
http://thisismysite.com/mySubSite

i made a custom list in sharepoint and called it USER INPUT, so thats the name of the list i use to call it and bind to the gridview!

running this it will show 4 columns in a grid. You could have many columns in a list, depending on what you want to show you would amend the ascx accordingly:

say iv added another column it would look like this

            <asp:BoundField DataField="ID" HeaderText="Request ref" />
            <asp:BoundField DataField="Title" HeaderText="Title" />
            <asp:BoundField DataField="Age" HeaderText="Business area" />   
            <asp:BoundField DataField="Status" HeaderText="Status" />   
            <asp:BoundField DataField="age" HeaderText="age" />    

now to know whats going on above, the DataField is the name of the field that is located within the list, HeaderText is what the column is going to be called in your webpart

i could do the following:

            <asp:BoundField DataField="ID" HeaderText="this is my id" />

instead of

            <asp:BoundField DataField="ID" HeaderText="Request ref" />

instead of visual webpart and going the webpart route:

within the creatchildcontrols of your .cs class:

    protected override void CreateChildControls()
    {
        if (!this.Page.IsPostBack)
        {
                //get the site from the current context
                SPSite site = SPContext.Current.Site;

                //get the root web
                SPWeb web = site.RootWeb;

                //query the list with the default view
                SPQuery query = new SPQuery();
                query.ViewXml = "<View/>";
                SPList list = web.Lists.TryGetList("USER INPUT");

                //get all the list items within the list
                SPListItemCollection items = list.GetItems(query);

                //create a grid view control 
                GridView gv = new GridView();
                //get the datatable from SPListItemCollection into GridView 
                gv.DataSource = items.GetDataTable();
                //bind the datatable
                gv.DataBind();

                //add the GridView control to the page
                this.Controls.Add(gv);

        }
    }

the above will bind the whole list!

to do as iv done in the ascx example you need to add some extra stuff:

    protected override void CreateChildControls()
    {
        if (!this.Page.IsPostBack)
        {
                //get the site from the current context
                SPSite site = SPContext.Current.Site;

                //get the root web
                SPWeb web = site.RootWeb;

                //query the list with the default view
                SPQuery query = new SPQuery();
                query.ViewXml = "<View/>";
                SPList list = web.Lists.TryGetList("USER INPUT");

                //get all the list items within the list
                SPListItemCollection items = list.GetItems(query);

                //create a grid view control 
                GridView gv = new GridView();
                //get the datatable from SPListItemCollection into GridView 
                gv.DataSource = items.GetDataTable();
                //bind the datatable

                gv.AutoGenerateColumns = false;
                gv.Columns.Add(new BoundField { DataField = "ID", HeaderText = "Request ref" });
                gv.Columns.Add(new BoundField { DataField = "Title", HeaderText = "Title" });
                gv.Columns.Add(new BoundField { DataField = "age", HeaderText = "age" });
                gv.Columns.Add(new BoundField { DataField = "Status", HeaderText = "Status" });

                gv.DataBind();

                //add the GridView control to the page
                this.Controls.Add(gv);

        }
    }

i dont know if you notice but what i added what the columns myself and said not to auto generate the columns:

                gv.AutoGenerateColumns = false;
                gv.Columns.Add(new BoundField { DataField = "ID", HeaderText = "Request ref" });
                gv.Columns.Add(new BoundField { DataField = "Title", HeaderText = "Title" });
                gv.Columns.Add(new BoundField { DataField = "age", HeaderText = "age" });
                gv.Columns.Add(new BoundField { DataField = "Status", HeaderText = "Status" });

hope it shines some light ;)

1

Yes you need to create a feature and add web part to it. About diference between WebPart and Visual WebPart: Short explanation: As far as usage is concerned, in many common cases there are almost no actual differences - anything you can do with a webpart you could do with a visual web part. For many points of view the visual web part is only a tool that enables the developer to work with a user control instead of creating all controls in codebehind.

Visual web part are born (afaik) from the SmartPart webpart project first seen on SharePoint 2007. The idea behind that project was to create a web part that could display any user control, so that programmer could work on ascx file as for any other web project instead to have to resort to programmatically create the controls via the CreateChildControl method.
The visual web part template introduced in Visual Studio 2010 just does this: it creates a simplified smartpart that displays the attached user control by adding it to the control collection in the CreateChildControl event. No more no less.

That said, beware that the visual web part does use a control, so it may cause issues in specific situation. First thing that comes to mind is that a visual web part is not supported in a sandbox solution (but you can use a modified template - see here or search for sandboxed visual web part in the visual studio gallery).

Waldek does a great job in is blog listing all the main differences: here is the link. What follows is just an extract of his notices, reposted to avoid link rooting. Also notice that as posted by almostSharepointMaster in his answer, you should also have a look at this page from Microsoft which also provides some samples.

Link to source Web Part / Visual Web Part comparision

1
  • Lol how did you get my old name ;) almostsharepointmaster
    – Ali Jafer
    Commented Dec 6, 2013 at 17:27
0

You will have to create a feature for this. (Or possibly add it to an existing feature)

WebParts only offer a code file in which you have to manually add your controls.

Visual Webparts allow you to use an HTML front end page in which you can organize your layout. A code behind file is also created to place your custom logic in. It follows the Webforms ASP.NET programming model.

3
  • So... I can ADD a LIST inside a Visual WebPart? If so, how?
    – JulianoG
    Commented Dec 6, 2013 at 14:24
  • First make sure your requirments justify the use of custom development. If you can achieve your needs with browser clicks, that's usually the best way to go. There are webparts provided by sharepoint which can be used to display lists with quite a bit UI customizations. For example the ListViewWebPart. Commented Dec 6, 2013 at 14:49
  • 1
    Check out ali's excellent post for an actual answer ;) Commented Dec 6, 2013 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.