Create Hidden SharePoint List for Site Owners

This article will show you how to create a custom hidden SharePoint List and to add a link in the Site Settings Administration Interface through a custom feature.

Many custom solutions for SharePoint require custom lists to store some application information or configuration. In SharePoint it’s very easy to create a Custom List and to use it to store application information. For example, you place a custom web part on your site that collects user information and stores this information in a Custom List. This logging information should be available only for site administration (or site owners). Ideally when you add specific lists for site administrators with a certain purpose, it is not recommended to place it under the “View All Site Content” page (_layouts/viewlsts.aspx), especially if you want to prevent users to view these list items. A good place will be the Galleries section on the Site Setting page and make the list hidden from your visitors/users.

This example shows how to add a hidden SharePoint List and Place a link to the list in the site setting page.

Creating a Feature Receiver in Visual Studio

1. Create a new C# project in Visual Studio and name it CustomListInSiteSettings.
2. Add Reference on the Project menu and select Microsoft.SharePoint.
3. Add new class and change the name of the class to CustomAdministrationList and make it inherit from the SPFeatureReceiver class, as follows:

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

namespace CustomListInSiteSettings

{

const string MY_CUSTOM_LIST = "MY CUSTOM LIST";

class CustomAdministrationList : SPFeatureReceiver

{

public override void FeatureActivated(SPFeatureReceiverProperties properties)

{

}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

{

}

public override void FeatureInstalled(SPFeatureReceiverProperties properties)

{

}

public override void FeatureUninstalling(SPFeatureReceiverProperties properties)

{

}

}

}



4. Add the following code within the class to override the FeatureActivated method.

SPWeb web = (SPWeb)properties.Feature.Parent;

SPListTemplateType genericList = new SPListTemplateType();

genericList = SPListTemplateType.GenericList;

//Check if the list exist

bool listExist = ContainList(web, MY_CUSTOM_LIST);

if (!listExist)

{

//Create a custom list

web.AllowUnsafeUpdates = true;

Guid listGuid = web.Lists.Add(MY_CUSTOM_LIST,

"List Description",

genericList);

//Hist the list

SPList list = web.Lists[listGuid];

list.Hidden = true;

//Add two text columns

SPFieldCollection collFields = list.Fields;

string field1 = collFields.Add("Column 1", SPFieldType.Text, false);

SPField column1 = collFields.GetFieldByInternalName(field1);

string field2 = collFields.Add("Column 2", SPFieldType.Text, false);

SPField column2 = collFields.GetFieldByInternalName(field2);

SPView view = list.DefaultView;

SPViewFieldCollection collViewFields = view.ViewFields;

collViewFields.Add(column1);

collViewFields.Add(column2);

view.Update();

//Remove permissions from the list

list.BreakRoleInheritance(false);

//Make site owners the list administrators

SPPrincipal principal = web.AssociatedOwnerGroup as SPPrincipal;

SPRoleAssignment assignment = new SPRoleAssignment(principal);

assignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.Administrator));

list.RoleAssignments.Add(assignment);

//update list changes

list.Update();



5. Click properties on the solution, click the cigning tab, select Sign the assembly, and choose a strong name key file, and then click .
6. In the create strong name Key dialog box, type CustomListInSiteSettings.snk in the Key file name box and then click OK.
7. Build the project.
8. In the \CustomListInSiteSettings\bin\Debug folder in the Visual Studio Projects folder, and drag the CustomListInSiteSettings.dll file to Local_Drive:\WINDOWS\assembly to place the DLL in the global assembly cache.


Add Link to Site Settings Page as a Feature
1. Create a folder in Local_Drive:/Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/FEATURES called CustomAdministrationList.
2. Create the Feature.xml file to provide the manifest for the feature, such as the following:

<?xml version="1.0" encoding="utf-8"?>

<Feature Id=" {YOUR_GUID}"

Title="CustomAdministrationList"

Description="This feature create a custom SharePoint List and dd it to the Site Settings Administration Interface"

Version="12.0.0.0"

Hidden="FALSE"

Scope="Web"

DefaultResourceFile="core"

ReceiverAssembly="CustomListInSiteSettings, Version=1.0.0.0, Culture=neutral, PublicKeyToken== {YOUR_ PublicKeyToken}"

ReceiverClass="CustomListInSiteSettings.CustomAdministrationList"

xmlns="http://schemas.microsoft.com/sharepoint/">

<ElementManifests>

<ElementManifest Location="elements.xml"/>

</ElementManifests>

</Feature>


3. To replace the GUID placeholder in the previous Id attribute, generate a GUID by running guidgen.exe located in the Local_Drive:\Program Files\Microsoft Visual Studio 8\Common7\Tools directory.
4. To get the Public Key Token of the assembly, in Windows Explorer find the DeletingEventHandler.dll file in the Local_Drive:\WINDOWS\assembly, right-click the file, click Properties, and on the General tab of the Properties dialog box, select and copy the token.
5. Create the Elements.xml file to define the link to redirect to the list. The URL action points to our custom list and the GroupId place it under the Galleries section. Such as the following:

<?xml version="1.0" encoding="utf-8" ?>

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

<CustomAction

Id="UserInterfaceCustomActions.SiteSettings"

GroupId="Galleries"

Location="Microsoft.SharePoint.SiteSettings"

Sequence="106"

Title="MY CUSTOM LIST">

<UrlAction Url="/Lists/MY%20CUSTOM%20LIST/AllItems.aspx"/>

</CustomAction>

</Elements>


Other common GroupId values that can be used in site settings page:
- Customization-- Under the Look and Feel section
- SiteAdministration – Under the Site Administration section
- UsersAndPermissions – Under the Users and Permissions section
- SiteCollectionAdmin - Under the Site Collection Administration section (if you use this GroupId, make sure you change the feature scope from Web to Site)

Adding List Items from Code

To add items to the custom list, we use the RunWithElevatedPrivileges method to executes the add item method with Full Control rights even if the user does not have Full Control.

The example creates a SPListItem object through the Add method of the collection. It then assigns values to the custom fields. Finally, the example calls the Update method of the list item to effect changes in the database.

SPSecurity.RunWithElevatedPrivileges(delegate()

{

using (SPSite site = new SPSite(SPContext.Current.Site.ID))

{

SPListItemCollection listItems = list.Items;

SPListItem item = listItems.Add();

item["Title"] = "my title";

item["Column 1"] = "my value 1";

item["Column 2"] = "my value 2";

item.Update();

}

});

Summery

Sometimes it is better to use SharePoint List as data source and not to connect to external database. When you do so, you need to make sure to assign unique permission to your list and to hide it from site users. This article shows you how to create a hidden SharePoint List, place a link on the Site Settings page and prevent users to view your list items.

By Alon Havivi   Popularity  (7182 Views)
Picture
Biography - Alon Havivi

Alon Havivi is a Microsoft Certified Technology Specialist, working as SharePoint Consultant / Developer at e-office. Specialized in SharePoint 2007/2010 and SharePoint Online (Office 365). With more than 10 years of experience in analysis, design and development complex Internet and Intranet portals using the latest Microsoft tools and practices, such as C# .NET 4.0, Silverlight and Windows Azure platform. Besides professional work, I write articles/blog and publish open source projects on CodePlex
View Alon Havivi's professional profile on LinkedIn. View Alon Havivi's projects on CodePlex. View Alon Havivi's articels on Eggheadcafe. Follow Alon Havivi on Twitter Connect with Alon Havivi via Facebook View Alon Havivi's Blog Subscribe to Alon Havivi RSS Feed