Thursday, August 18, 2011

Sharepoint 2010: show "Add new item" or "Add new" for custom ListViewWebPart

First create a application page and copy below code. you can access this page like this.


.CS Page
public partial class Test : LayoutsPageBase
        protected void Page_Load(object sender, EventArgs e)


                    using (SPWeb oHomeWeb = new SPSite(SPContext.Current.Site.Url).OpenWeb())
                        oHomeWeb.AllowUnsafeUpdates = true;
                        SPList oWorkingDocumentsList = oHomeWeb.Lists["Announcements"];
                        SPView oCustomView = oWorkingDocumentsList.Views["All items"];
                        oCustomView.RowLimit = 5;

                        XsltListViewWebPart lvwpShowFiles = new XsltListViewWebPart();
                        lvwpShowFiles.ID = "wpListView";
                        lvwpShowFiles.Title = "Announcements using XsltListViewWebPart";
                        lvwpShowFiles.ChromeType = PartChromeType.TitleAndBorder;
                        lvwpShowFiles.AllowEdit = true;
                        lvwpShowFiles.AllowConnect = true;
                        lvwpShowFiles.ListName = oWorkingDocumentsList.ID.ToString("B").ToUpper();
                        lvwpShowFiles.ViewGuid = oCustomView.ID.ToString("B").ToUpper();

                        PropertyInfo nodeProp = oCustomView.GetType().GetProperty("Node", BindingFlags.NonPublic | BindingFlags.Instance);
                        XmlNode node = nodeProp.GetValue(oCustomView, null) as XmlNode;
                        XmlNode toolbarNode = node.SelectSingleNode("Toolbar");

                        if (toolbarNode != null)
                            if (!toolbarNode.Attributes["Type"].Value.Equals("Freeform"))
                                toolbarNode.Attributes["Type"].Value = "Freeform";

                        dvSummaryView.Controls.Add(lvwpShowFiles); //create div controls in a aspx page
                        oHomeWeb.AllowUnsafeUpdates = false;

            catch (Exception ex)
                throw ex;

.ASPX Page
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="OPSSitePages.Layouts.OPS.Test" DynamicMasterPageFile="~masterurl/default.master" %>

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">


<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<div id="dvSummaryView" runat="server"></div>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
Application Page

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
My Application Page

Please refer below post as well


  1. you are leaving your site to cross script attack in case your code fails it will leave spweb to allowunsafe updates to true. it should be set to false in all times.

  2. Hi dude good job. i receive an error "the object specified does not belong to a list" after i create a new folder and click on it.

  3. Hi you given a very useful code. When I used your code everything worked well but there is a problem when adding an announcement item from the 2nd page of the announcement webpart, when you add an item it is added but when we move to 1st page it is not coming one has to refresh the page to get the newly added item. At the same time, When we add a new item from the first page it is added without any refresh of the page. Can you infer anything.



Blog Archive