2

I am currently using Sharepoint 2013 foundation. I created a custom Web Part in Visual Studio to help filter a custom list on logged in users. Each logged in user will see a separate view. I then add the custom web part to a page, and it works great.

The only issue is it keeps the '+ new item' option and it adds a light gray border around the web part.

Would it be possible to add code to the custom web part to remove the '+ add new' and to remove the gray border.

Below is the c# for my custom web part:

using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Collections.Generic;
using System.ComponentModel;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Administration;
using System.Collections;

namespace Custom_Webparts.Stab_List_Summary
{
    [ToolboxItemAttribute(false)]
    public partial class Stab_List_Summary : WebPart
    {
        // Uncomment the following SecurityPermission attribute only when doing         Performance Profiling on a farm solution
        // using the Instrumentation method, and then remove the SecurityPermission     attribute when the code is ready
        // for production. Because the SecurityPermission attribute bypasses the security check for callers of
    // your constructor, it's not recommended for production purposes.
    // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
    public DCS_Stab_List_Summary()
    {
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitializeControl();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        using (SPSite site = new SPSite("http://sitename")) // This URL needs to be changed to appropriate URL. 
        {
            using (SPWeb web = site.OpenWeb())
            {
                // refer to the Stab List
                SPList olist = web.Lists["Stab List"]; // "Stab List" needs to be changed to appropriate list name

                //new XSLT webpart
                XsltListViewWebPart webpart = new XsltListViewWebPart();

                //custom XSLT Listview webpart properties
                webpart.ListName = olist.ID.ToString("B").ToUpper();
                webpart.AllowEdit = true;
                webpart.AllowConnect = true;
                webpart.AllowZoneChange = true;

                //Refer views for different user
                SPView oTView = olist.Views["Summary T"];
                SPView oEView = olist.Views["Summary E"];
                SPView oNormalView = olist.Views["Summary"];

                //getthe logged in user name
                SPUser oUser = SPContext.Current.Web.CurrentUser;


                //If logged in user is T_sp, then apply appropriate view
                if (oUser.Name.Contains("T_sp")) // t_sp
                {
                    webpart.ViewGuid = oTView.ID.ToString("B");
                }
                else if (oUser.Name.Contains("E_sp")) // e_sp;
                {
                    webpart.ViewGuid = oEView.ID.ToString("B");
                }
                // for other supply default view
                else
                {
                    webpart.ViewGuid = oNormalView.ID.ToString("B");
                }

                this.Controls.Add(webpart);
                }
            }
        }        
    }
}

Here is a link to show an image of the gray border I am speaking about: screenshot

1

You can loop through the XsltListViewWebPart internal controls and hide the Toolbar control like:

//Find the ToolBar control and set visible to False 
foreach (Control ctrl in webpart.Controls) 
{ 
  if (ctrl.GetType() == typeof(ViewToolBar)) 
  { 
    ctrl.Visible = false; 
    break; 
  } 
}

Or you can use Reflection to set the Toolbar to none for View, as shown in
SharePoint: Programatically add XSLTList view webpart to page

However which Grey border are you referring to? Can you add a snapshot

  • I will try the code you put above and let you know the results. So basically, I paste the code you provided right below my code that says //new XSLT webpartXsltListVewWebPart webpart = new XsltListViewWebPart(); ? I have edited my original post to show the image of the gray border. – Caleb Fortner Oct 31 '14 at 19:59
  • Sorry, this is new to me. When I paste your coding into the XsltListViewWebPart section, it tells me that "The name '_wpListView' does not exist in the current context". Any ideas on what I am missing? – Caleb Fortner Oct 31 '14 at 21:03
  • @CalebFortner the _wpListView should be replaced with your XsltListViewWebPart object i.e. webpart – Arsalan Adam Khatri Oct 31 '14 at 22:29
  • I hate to be a pain, but like I said, this is all new to me. So I currently have 'XsltListViewWebPart webpart = new XsltListViewWebPart();', then you gave me 'foreach (Control ctrl in webpart.Controls) { if (ctrl.GetType() == typeof(ViewToolBar)) { ctrl.Visible = false; break; } }'. What should my new code look like? – Caleb Fortner Oct 31 '14 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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