Create Hidden SharePoint List for Site Owners
[LEFT][CODE]http://www.eggheadcafe.com/tutorials/aspnet/585c3b6b-a727-4e73-a860-917a85a41b08/create-hidden-sharepoint.aspx[/CODE]
[IMG]http://www.eggheadcafe.com/fileupload/-2010403606_havivi.jpg[/IMG]
[B]Alon Havivi[/B]
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.
[IMG]http://eggheadcafe.com/FileUpload/-1253398195_img1.JPG[/IMG]
[B]Creating a Feature Receiver in Visual Studio[/B]
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:
[COLOR=Blue][FONT=Courier New]using[/FONT][/COLOR][FONT=Courier New] System;[/FONT]
[COLOR=Blue][FONT=Courier New]using[/FONT][/COLOR][FONT=Courier New] System.Collections.Generic;[/FONT]
[COLOR=Blue][FONT=Courier New]using[/FONT][/COLOR][FONT=Courier New] System.Text;[/FONT]
[COLOR=Blue][FONT=Courier New]using[/FONT][/COLOR][FONT=Courier New] Microsoft.SharePoint;[/FONT]
[FONT=Courier New][/FONT]
[COLOR=Blue][FONT=Courier New]namespace[/FONT][/COLOR][FONT=Courier New] CustomListInSiteSettings[/FONT]
[FONT=Courier New]{[/FONT]
[COLOR=Blue][FONT=Courier New]const[/FONT][/COLOR][FONT=Courier New] [COLOR=Blue]string[/COLOR] MY_CUSTOM_LIST = [COLOR=#A31515]"MY CUSTOM LIST"[/COLOR];[/FONT]
[FONT=Courier New][/FONT]
[FONT=Courier New][COLOR=Blue]class[/COLOR] [COLOR=#2B91AF]CustomAdministrationList[/COLOR] : [COLOR=#2B91AF]SPFeatureReceiver[/COLOR][/FONT]
[FONT=Courier New]{[/FONT]
[FONT=Courier New][COLOR=Blue]public[/COLOR] [COLOR=Blue]override[/COLOR] [COLOR=Blue]void[/COLOR] FeatureActivated([COLOR=#2B91AF]SPFeatureReceiverProperties[/COLOR] properties)[/FONT]
[FONT=Courier New]{ [/FONT]
[FONT=Courier New]}[/FONT]
[FONT=Courier New][/FONT]
[FONT=Courier New][COLOR=Blue]public[/COLOR] [COLOR=Blue]override[/COLOR] [COLOR=Blue]void[/COLOR] FeatureDeactivating([COLOR=#2B91AF]SPFeatureReceiverProperties[/COLOR] properties)[/FONT]
[FONT=Courier New]{ [/FONT]
[FONT=Courier New]}[/FONT]
[FONT=Courier New][/FONT]
[FONT=Courier New][COLOR=Blue]public[/COLOR] [COLOR=Blue]override[/COLOR] [COLOR=Blue]void[/COLOR] FeatureInstalled([COLOR=#2B91AF]SPFeatureReceiverProperties[/COLOR] properties)[/FONT]
[FONT=Courier New]{ [/FONT]
[FONT=Courier New]}[/FONT]
[FONT=Courier New][/FONT]
[FONT=Courier New][COLOR=Blue]public[/COLOR] [COLOR=Blue]override[/COLOR] [COLOR=Blue]void[/COLOR] FeatureUninstalling([COLOR=#2B91AF]SPFeatureReceiverProperties[/COLOR] properties)[/FONT]
[FONT=Courier New]{ [/FONT]
[FONT=Courier New]}[/FONT]
[FONT=Courier New]}[/FONT]
[FONT=Courier New]}[/FONT]
4. Add the following code within the class to override the FeatureActivated method.
[COLOR=#2B91AF][FONT=Courier New]SPWeb[/FONT][/COLOR][FONT=Courier New] web = ([COLOR=#2B91AF]SPWeb[/COLOR])properties.Feature.Parent;[/FONT]
[FONT=Courier New][COLOR=#2B91AF]SPListTemplateType[/COLOR] genericList = [COLOR=Blue]new[/COLOR] [COLOR=#2B91AF]SPListTemplateType[/COLOR]();[/FONT]
[FONT=Courier New]genericList = [COLOR=#2B91AF]SPListTemplateType[/COLOR].GenericList;[/FONT]
[FONT=Courier New][/FONT]
[FONT=Courier New][COLOR=Green]//Check if the list exist[/COLOR][/FONT]
[FONT=Courier New][COLOR=Blue]bool[/COLOR] listExist = ContainList(web, MY_CUSTOM_LIST);[/FONT]
[FONT=Courier New][/FONT]
[FONT=Courier New][/FONT]
[FONT=Courier New][COLOR=Blue]if[/COLOR] (!listExist)[/FONT]
[FONT=Courier New]{[/FONT]
[FONT=Courier New][COLOR=Green]//Create a custom list[/COLOR][/FONT]
[FONT=Courier New]web.AllowUnsafeUpdates = [COLOR=Blue]true[/COLOR];[/FONT]
[FONT=Courier New][COLOR=#2B91AF]Guid[/COLOR] listGuid = web.Lists.Add(MY_CUSTOM_LIST,[/FONT]
[FONT=Courier New][COLOR=#A31515]"List Description"[/COLOR],[/FONT]
[FONT=Courier New]genericList);[/FONT]
[FONT=Courier New][/FONT]
[FONT=Courier New][COLOR=Green]//Hist the list[/COLOR][/FONT]
[FONT=Courier New][COLOR=#2B91AF]SPList[/COLOR] list = web.Lists
[listGuid];[/FONT]
[FONT=Courier New]list.Hidden = [COLOR=Blue]true[/COLOR];[/FONT]
[FONT=Courier New][/FONT]
[FONT=Courier New][/FONT]
[FONT=Courier New][COLOR=Green]//Add two text columns[/COLOR][/FONT]
[FONT=Courier New][COLOR=#2B91AF]SPFieldCollection[/COLOR] collFields = list.Fields;[/FONT]
[FONT=Courier New][/FONT]
[FONT=Courier New][COLOR=Blue]string[/COLOR] field1 = collFields.Add([COLOR=#A31515]"Column 1"[/COLOR], [COLOR=#2B91AF]SPFieldType[/COLOR].Text, [COLOR=Blue]false[/COLOR]);[/FONT]
[FONT=Courier New][COLOR=#2B91AF]SPField[/COLOR] column1 = collFields.GetFieldByInternalName(field1);[/FONT]
[FONT=Courier New][/FONT]
[FONT=Courier New][COLOR=Blue]string[/COLOR] field2 = collFields.Add([COLOR=#A31515]"Column 2"[/COLOR], [COLOR=#2B91AF]SPFieldType[/COLOR].Text, [COLOR=Blue]false[/COLOR]);[/FONT]
[FONT=Courier New][COLOR=#2B91AF]SPField[/COLOR] column2 = collFields.GetFieldByInternalName(field2);[/FONT]
[FONT=Courier New][/FONT]
[FONT=Courier New][COLOR=#2B91AF]SPView[/COLOR] view = list.DefaultView;[/FONT]
[FONT=Courier New][COLOR=#2B91AF]SPViewFieldCollection[/COLOR] collViewFields = view.ViewFields;[/FONT]
[FONT=Courier New][/FONT]
[FONT=Courier New]collViewFields.Add(column1);[/FONT]
[FONT=Courier New]collViewFields.Add(column2);[/FONT]
[FONT=Courier New]view.Update();[/FONT]
[FONT=Courier New][/FONT]
[FONT=Courier New][COLOR=Green]//Remove permissions from the list[/COLOR][/FONT]
[FONT=Courier New]list.BreakRoleInheritance([COLOR=Blue]false[/COLOR]);[/FONT]
[FONT=Courier New][/FONT]
[FONT=Courier New][COLOR=Green]//Make site owners the list administrators [/COLOR][/FONT]
[FONT=Courier New][COLOR=#2B91AF]SPPrincipal[/COLOR] principal = web.AssociatedOwnerGroup [COLOR=Blue]as[/COLOR] [COLOR=#2B91AF]SPPrincipal[/COLOR]; [/FONT]
[FONT=Courier New][COLOR=#2B91AF]SPRoleAssignment[/COLOR] assignment = [COLOR=Blue]new[/COLOR] [COLOR=#2B91AF]SPRoleAssignment[/COLOR](principal);[/FONT]
[FONT=Courier New]assignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType([COLOR=#2B91AF]SPRoleType[/COLOR].Administrator)); [/FONT]
[FONT=Courier New]list.RoleAssignments.Add(assignment);[/FONT]
[FONT=Courier New][/FONT]
[FONT=Courier New][COLOR=Green]//update list changes[/COLOR][/FONT]
[FONT=Courier New]list.Update();[/FONT]
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.
[B]
Add Link to Site Settings Page as a Feature
[/B]
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:
[COLOR=Blue][FONT=Courier New]<?[/FONT][/COLOR][COLOR=#A31515][FONT=Courier New]xml[/FONT][/COLOR][COLOR=Blue][FONT=Courier New] [/FONT][/COLOR][COLOR=Red][FONT=Courier New]version[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]=[/FONT][/COLOR][FONT=Courier New]"[COLOR=Blue]1.0[/COLOR]"[COLOR=Blue] [/COLOR][COLOR=Red]encoding[/COLOR][COLOR=Blue]=[/COLOR]"[COLOR=Blue]utf-8[/COLOR]"[COLOR=Blue]?>[/COLOR][/FONT]
[COLOR=Blue][FONT=Courier New]<[/FONT][/COLOR][COLOR=#A31515][FONT=Courier New]Feature[/FONT][/COLOR][COLOR=Blue][FONT=Courier New] [/FONT][/COLOR][COLOR=Red][FONT=Courier New]Id[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]=[/FONT][/COLOR][FONT=Courier New]"[/FONT] {[COLOR=Blue][FONT=Courier New]YOUR[/FONT][/COLOR]_[COLOR=Blue][FONT=Courier New]GUID}[/FONT][/COLOR][FONT=Courier New]"[/FONT]
[COLOR=Blue][FONT=Courier New][/FONT][/COLOR][COLOR=Red][FONT=Courier New]Title[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]=[/FONT][/COLOR][FONT=Courier New]"[COLOR=Blue]CustomAdministrationList[/COLOR]"[/FONT]
[COLOR=Blue][FONT=Courier New][/FONT][/COLOR][COLOR=Red][FONT=Courier New]Description[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]=[/FONT][/COLOR][FONT=Courier New]"[COLOR=Blue]This feature create a custom SharePoint List and dd it to the Site Settings Administration Interface[/COLOR]"[/FONT]
[COLOR=Blue][FONT=Courier New][/FONT][/COLOR][COLOR=Red][FONT=Courier New]Version[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]=[/FONT][/COLOR][FONT=Courier New]"[COLOR=Blue]12.0.0.0[/COLOR]"[/FONT]
[COLOR=Blue][FONT=Courier New][/FONT][/COLOR][COLOR=Red][FONT=Courier New]Hidden[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]=[/FONT][/COLOR][FONT=Courier New]"[COLOR=Blue]FALSE[/COLOR]"[/FONT]
[COLOR=Blue][FONT=Courier New][/FONT][/COLOR][COLOR=Red][FONT=Courier New]Scope[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]=[/FONT][/COLOR][FONT=Courier New]"[COLOR=Blue]Web[/COLOR]"[/FONT]
[COLOR=Blue][FONT=Courier New][/FONT][/COLOR][COLOR=Red][FONT=Courier New]DefaultResourceFile[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]=[/FONT][/COLOR][FONT=Courier New]"[COLOR=Blue]core[/COLOR]"[/FONT]
[COLOR=Blue][FONT=Courier New][/FONT][/COLOR][COLOR=Red][FONT=Courier New]ReceiverAssembly[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]=[/FONT][/COLOR][FONT=Courier New]"[COLOR=Blue]CustomListInSiteSettings, Version=1.0.0.0, Culture=neutral, PublicKeyToken==[/COLOR][/FONT] {[COLOR=Blue][FONT=Courier New]YOUR[/FONT][/COLOR]_[COLOR=Blue][FONT=Courier New] PublicKeyToken}[/FONT][/COLOR][FONT=Courier New]"[/FONT]
[COLOR=Blue][FONT=Courier New][/FONT][/COLOR][COLOR=Red][FONT=Courier New]ReceiverClass[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]=[/FONT][/COLOR][FONT=Courier New]"[COLOR=Blue]CustomListInSiteSettings.CustomAdministrationList[/COLOR]"[COLOR=Blue] [/COLOR][/FONT]
[COLOR=Blue][FONT=Courier New][/FONT][/COLOR][COLOR=Red][FONT=Courier New]xmlns[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]=[/FONT][/COLOR][FONT=Courier New]"[COLOR=Blue]http://schemas.microsoft.com/sharepoint/[/COLOR]"[COLOR=Blue]>[/COLOR][/FONT]
[COLOR=Blue][FONT=Courier New]<[/FONT][/COLOR][COLOR=#A31515][FONT=Courier New]ElementManifests[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]>[/FONT][/COLOR]
[COLOR=Blue][FONT=Courier New]<[/FONT][/COLOR][COLOR=#A31515][FONT=Courier New]ElementManifest[/FONT][/COLOR][COLOR=Blue][FONT=Courier New] [/FONT][/COLOR][COLOR=Red][FONT=Courier New]Location[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]=[/FONT][/COLOR][FONT=Courier New]"[COLOR=Blue]elements.xml[/COLOR]"[COLOR=Blue]/>[/COLOR][/FONT]
[COLOR=Blue][FONT=Courier New]</[/FONT][/COLOR][COLOR=#A31515][FONT=Courier New]ElementManifests[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]>[/FONT][/COLOR]
[COLOR=Blue][FONT=Courier New]</[/FONT][/COLOR][COLOR=#A31515][FONT=Courier New]Feature[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]>[/FONT][/COLOR]
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:
[COLOR=Blue][FONT=Courier New]<?[/FONT][/COLOR][COLOR=#A31515][FONT=Courier New]xml[/FONT][/COLOR][COLOR=Blue][FONT=Courier New] [/FONT][/COLOR][COLOR=Red][FONT=Courier New]version[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]=[/FONT][/COLOR][FONT=Courier New]"[COLOR=Blue]1.0[/COLOR]"[COLOR=Blue] [/COLOR][COLOR=Red]encoding[/COLOR][COLOR=Blue]=[/COLOR]"[COLOR=Blue]utf-8[/COLOR]"[COLOR=Blue] ?>[/COLOR][/FONT]
[COLOR=Blue][FONT=Courier New]<[/FONT][/COLOR][COLOR=#A31515][FONT=Courier New]Elements[/FONT][/COLOR][COLOR=Blue][FONT=Courier New] [/FONT][/COLOR][COLOR=Red][FONT=Courier New]xmlns[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]=[/FONT][/COLOR][FONT=Courier New]"[COLOR=Blue]http://schemas.microsoft.com/sharepoint/[/COLOR]"[COLOR=Blue]>[/COLOR][/FONT]
[COLOR=Blue][FONT=Courier New]<[/FONT][/COLOR][COLOR=#A31515][FONT=Courier New]CustomAction[/FONT][/COLOR]
[COLOR=Blue][FONT=Courier New][/FONT][/COLOR][COLOR=Red][FONT=Courier New]Id[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]=[/FONT][/COLOR][FONT=Courier New]"[COLOR=Blue]UserInterfaceCustomActions.SiteSettings[/COLOR]"[/FONT]
[COLOR=Blue][FONT=Courier New][/FONT][/COLOR][COLOR=Red][FONT=Courier New]GroupId[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]=[/FONT][/COLOR][FONT=Courier New]"[COLOR=Blue]Galleries[/COLOR]"[/FONT]
[COLOR=Blue][FONT=Courier New][/FONT][/COLOR][COLOR=Red][FONT=Courier New]Location[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]=[/FONT][/COLOR][FONT=Courier New]"[COLOR=Blue]Microsoft.SharePoint.SiteSettings[/COLOR]"[/FONT]
[COLOR=Blue][FONT=Courier New][/FONT][/COLOR][COLOR=Red][FONT=Courier New]Sequence[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]=[/FONT][/COLOR][FONT=Courier New]"[COLOR=Blue]106[/COLOR]"[/FONT]
[COLOR=Blue][FONT=Courier New][/FONT][/COLOR][COLOR=Red][FONT=Courier New]Title[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]=[/FONT][/COLOR][FONT=Courier New]"[COLOR=Blue]MY CUSTOM LIST[/COLOR]"[COLOR=Blue]>[/COLOR][/FONT]
[COLOR=Blue][FONT=Courier New]<[/FONT][/COLOR][COLOR=#A31515][FONT=Courier New]UrlAction[/FONT][/COLOR][COLOR=Blue][FONT=Courier New] [/FONT][/COLOR][COLOR=Red][FONT=Courier New]Url[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]=[/FONT][/COLOR][FONT=Courier New]"[COLOR=Blue]/Lists/MY%20CUSTOM%20LIST/AllItems.aspx[/COLOR]"[COLOR=Blue]/>[/COLOR][/FONT]
[COLOR=Blue][FONT=Courier New]</[/FONT][/COLOR][COLOR=#A31515][FONT=Courier New]CustomAction[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]>[/FONT][/COLOR]
[COLOR=Blue][FONT=Courier New]</[/FONT][/COLOR][COLOR=#A31515][FONT=Courier New]Elements[/FONT][/COLOR][COLOR=Blue][FONT=Courier New]>[/FONT][/COLOR]
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)
[B]Adding List Items from Code[/B]
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.
[COLOR=#2B91AF][FONT=Courier New]SPSecurity[/FONT][/COLOR][FONT=Courier New].RunWithElevatedPrivileges([COLOR=Blue]delegate[/COLOR]()[/FONT]
[FONT=Courier New]{[/FONT]
[FONT=Courier New][COLOR=Blue]using[/COLOR] ([COLOR=#2B91AF]SPSite[/COLOR] site = [COLOR=Blue]new[/COLOR] [COLOR=#2B91AF]SPSite[/COLOR]([COLOR=#2B91AF]SPContext[/COLOR].Current.Site.ID))[/FONT]
[FONT=Courier New]{[/FONT]
[FONT=Courier New][COLOR=#2B91AF]SPListItemCollection[/COLOR] listItems = list.Items;[/FONT]
[FONT=Courier New][COLOR=#2B91AF]SPListItem[/COLOR] item = listItems.Add();[/FONT]
[FONT=Courier New]item[[COLOR=#A31515]"Title"[/COLOR]] = [COLOR=#A31515]"my title"[/COLOR];[/FONT]
[FONT=Courier New]item[[COLOR=#A31515]"Column 1"[/COLOR]] = [COLOR=#A31515]"my value 1"[/COLOR];[/FONT]
[FONT=Courier New]item[[COLOR=#A31515]"Column 2"[/COLOR]] = [COLOR=#A31515]"my value 2"[/COLOR];[/FONT]
[FONT=Courier New]item.Update();[/FONT]
[FONT=Courier New]}[/FONT]
[FONT=Courier New]});
[/FONT]
[B]Summery[/B]
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.
[B][URL="http://eggheadcafe.com/FileUpload/-1253398195_CustomListInSiteSettings.zip"]Download the solution with source code[/URL][/B]
[/LEFT]