We often have to create solutions involving hidden lists and libraries. If our hidden list is part of a SharePoint feature, we would probably handle the logic of hiding the list during our feature activation routine. But what if the solution is not part of a feature? Maybe the lists where created using the browser. What if we need to make our hidden list visible to help troubleshoot something that has gone wrong?

I’ve create a SharePoint Application Page that uses the SharePoint Object Model and a series of ASP.Net controls to display and modify properties of SharePoint Lists and Fields in a site. For security reasons only users with full control of the site can access the page.

The user can select the list from an ASP.Net list control. Upon selecting the list, the page exposes the Hidden, AllowDeletion, RequestAccessEnabled, and AllowEveryoneViewItems properties of the selected list.

image

I expose the list fields during the same time (when the user selects a list.) Upon selecting a field from the list, the page exposes the Hidden property of the selected field.

image

The following is the code for the application page:

private const string webAppPropertyKey = “Custom404Path”;

protected override void OnLoad(EventArgs e)
{
    using (SPWeb site = SPContext.Current.Site.OpenWeb())
    {
        //****************************************
        // Validate the page request to avoid
        // any malicious posts
        if (Request.HttpMethod == “POST”)
            SPUtility.ValidateFormDigest();

        //****************************************
        // Get a reference the roles that are
        // bound to the user and the role
        // definition to which we need to verify
        // the user against
        SPRoleDefinitionBindingCollection usersRoles = site.AllRolesForCurrentUser;
        SPRoleDefinitionCollection siteRoleCollection = site.RoleDefinitions;
        SPRoleDefinition roleDefinition = siteRoleCollection[“Full Control”];

        //Check if the user is in the role
        if ((usersRoles.Contains(roleDefinition)) || site.CurrentUser.IsSiteAdmin)
        {
            //*******************************
            //Check if post back to run
            //code that initiates the page
            if (IsPostBack != true)
            {
                InitPage();
            }
        }
        else
        {
            Response.Redirect(“/_layouts/accessdenied.aspx”);
        }
    }
}

//*********************************************
// This method populates the listbox with
// all of the lists(SPList) in the site(SPWeb)
public void InitPage()
{
    //Get a reference to the SPWeb object
    using (SPWeb site = SPContext.Current.Site.OpenWeb())
    {
        //Iterate through each of the lists(SPList) in the site
        foreach (SPList thislist in site.Lists)
        {
            //Create a listItem for each list.
            //Set the “text” property to the title
            //and the value to the list ID
            //we will use the list ID to retrieve the
            //list settings when the user selects it
            ListItem item = new ListItem(thislist.Title, thislist.ID.ToString());
            lstSiteLists.Items.Add(item);
        }
    }
}

//*************************************************
// This method retrieves some of the list settings
// that are not accessible via the site or list settings
// pages on the SharePoint site
public void GetListSettings(object sender, System.EventArgs e)
{
    //make the list settings panel visible
    //and enable to the “Update List” button
    pnlListSettings.Visible = true;
    btnUpdateList.Enabled = true;

    //get the ID of the selected list
    Guid listGuid = new Guid(lstSiteLists.SelectedValue);

    //get a reference to the site(SPWeb) object
    using (SPWeb site = SPContext.Current.Site.OpenWeb())
    {
        //get a reference to the list object using
        //the list ID
        SPList list = site.Lists[listGuid];

        //Retrieve and display the ID, template name,
        //and template type of the list
        lblListGUID.Text = list.ID.ToString();
        lblListBaseTemplate.Text = list.BaseTemplate.ToString();
        lblListBaseType.Text = list.BaseType.ToString();

        //check the “Hidden”, “AllowDelete”, “RequestAccessEnabled”
        //and “AllowEveryOneviewItems” properties of the list and
        //update the corresponding checkboxes accordingly
        chkListHidden.Checked = list.Hidden;
        chkListAllowDelete.Checked = list.AllowDeletion;
        chkListAllowRequestAccess.Checked = list.RequestAccessEnabled;
        chkAllowEveryoneViewItems.Checked = list.AllowEveryoneViewItems;

        //Disable the AllowRequest access checkbox if
        //the list is inheriting permissions as this
        //will be inherited as well
        if (list.Permissions.Inherited)
        {
            chkListAllowRequestAccess.Enabled = false;
        }
        else
        {
            chkListAllowRequestAccess.Enabled = true;
        }

        //Call the method that populates the “Field” listbox
        //with the fields(SPField) that are available in the selected
        //list
        GetListFields(list);
    }
}

//**************************************************
//This method populates the “Field” List box with
//the fields(SPField) that are available in the
//list(SPList) that is passed in
public void GetListFields(SPList list)
{
    //clear the list box before populating it
    //(the user may have selected a new list)
    lstFields.Items.Clear();

    //hide the field settings panel as
    //the user will need to select a new list
    pnlFieldSettings.Visible = false;

    //disable the update field button
    //as the user will need to select a new list
    btnUpdateField.Enabled = false;

    //iterate through each field(SPField) in the list(SPList)
    foreach (SPField field in list.Fields)
    {
        //Create a listItem for each field.
        //Set the “text” property to the title
        //and the value to the field ID
        //we will use the field ID to retrieve the
        //fields settings when the user selects it
        ListItem item = new ListItem(field.Title + ” [” + field.InternalName + “]”,         field.Id.ToString());
        lstFields.Items.Add(item);
    }
}

//*************************************************
// This method retrieves some of the field settings
// that are not accessible via the site or list settings
// pages on the SharePoint site
public void GetFieldSettings(object sender, System.EventArgs e)
{
    //Make the field settings panel visible
    //and enable the field settings button
    pnlFieldSettings.Visible = true;
    btnUpdateField.Enabled = true;

    //Get the ID of the selected list and
    //the ID of the field. We will use these
    //to retrieve the list and field settings
    Guid listGuid = new Guid(lstSiteLists.SelectedValue);
    Guid fieldGuid = new Guid(lstFields.SelectedValue);

    using (SPWeb site = SPContext.Current.Site.OpenWeb())
    {
        //get a reference to the list and field
        //from the list.
        SPList list = site.Lists[listGuid];
        SPField field = list.Fields[fieldGuid];

        //display the Field ID and internal name
        lblFieldID.Text = field.Id.ToString();
        lblFieldInternalName.Text = field.InternalName; 

        //check the CanToggleHidden Attribute of the field
        //and if false display a warning
        if (!(field.CanToggleHidden))
        {
            lblFieldWarning.Text = “Warning: The CanToggleHidden property of this field has been set to false; changing its \”hidden\” property could render your application unstable.”;
         }
         else
         {
             lblFieldWarning.Text = “”;
         }

        //retrieve the “hidden” property value
        //of the field and set the corresponding
        //checkbox
        chkFieldHidden.Checked = field.Hidden;
    }
}

//**************************************************
//This method updates the list settings according
//to the values specified in the form
public void UpdateList(object sender, System.EventArgs e)
{
    //Get the ID of the selected list
    Guid listGuid = new Guid(lstSiteLists.SelectedValue);
    using (SPWeb site = SPContext.Current.Site.OpenWeb())
    {
        SPList list = site.Lists[listGuid];
        list.AllowDeletion = chkListAllowDelete.Checked;
        if (!(list.Permissions.Inherited))
        {
            list.RequestAccessEnabled = chkListAllowRequestAccess.Checked;
        }
        list.Hidden = chkListHidden.Checked;
        list.AllowEveryoneViewItems = chkAllowEveryoneViewItems.Checked;

        list.Update();
        site.AllowUnsafeUpdates = false;
    }
}

public void UpdateField(object sender, System.EventArgs e)
{
    Guid listGuid = new Guid(lstSiteLists.SelectedValue);
    Guid fieldGuid = new Guid(lstFields.SelectedValue);

    using (SPWeb site = SPContext.Current.Site.OpenWeb())
    {
        SPList list = site.Lists[listGuid];
        SPField field = list.Fields[fieldGuid];

        lblFieldID.Text = field.Id.ToString();
        lblFieldInternalName.Text = field.InternalName;

        if (!(field.CanToggleHidden))
        {
            Type type = field.GetType();
            MethodInfo mi = type.GetMethod(“SetFieldBoolValue”, BindingFlags.NonPublic | BindingFlags.Instance);
            mi.Invoke(field, new object[] { “CanToggleHidden”, true });
        }

        field.Hidden = chkFieldHidden.Checked;
        field.Update();

    }
}

 

   

 

 

   

   

 

    Select List:

   
   

       
        List Settings

        ID:
       

        Base Template:
       

        Base Type:
       

       

       

        (must first turn off permission inheritance)

       

       
       
   

        Fields

       
   

       
        Field Settings

        ID:
       

        Internal Name:
       

       

       

       

       
       
   

    Advanced List Settings

    Advanced List Settings

Click here to download a zip file containing the Application Page.

 

Updated 8/26/2008: Changed how I obtained the reference to the SPWeb object throughout the code. The code was originally obtaining a reference to the SPWeb object via the current context from within a “using” statement. This was wrong as disposing of a shared resource such as an SPWeb object obtained from the current context may cause the SharePoint object model to behave unpredictably. Alternatively I could have removed the using statements and let SharePoint manage the object, but considering the number of lines of code it was easier to do a find and replace from “this.Web” to “SPContext.Current.Site.OpenWeb()” which does not return a reference to a shared resource. See Best Practices: Using Disposable Windows SharePoint Services Objects for more information on the subject.

Technorati Tags: SharePoint, WSS, MOSS, ASP, NET

Advertisements