Sorting and Filtering Lists : Page 3

Sorting and Filtering Lists

You can sort the tasks you created earlier. As an example, you can change the sort order (see Figure 14).


Figure 14. Sorting Tasks: Here, the items in the Sample list have been sorted by due date.

You can also filter the display of the tasks you have created. For example, you might set a filter for the "Task Category," (see Figure 15,) ensuring that only the tasks in the "Software Development" category show up (see Figure 16).


Figure 16 shows the results—which displays only one task, because only that task has been assigned to the Software Development category.

Restricting Access to Lists


Figure 17. Setting Permissions: To restrict access to the Sample list, click "Permissions for this list" link.

You can also restrict access to the lists—allowing or restricting a particular site user's access to the list. For example to restrict access, follow these steps:

  1. Click on Settings → List Settings
  2. In the Permissions and Management section, click "Permissions for this list" (see Figure 17).
  • Now, click on Actions --> Edit Permissions to edit the list permissions (see Figure 18).


  • Click the "Edit Permissions" item to restrict access to the Sample List.

    You can add or remove user permissions to enable or restrict access to the list, as shown in Figure 19.


    Figure 19. Select Users: Select the accounts that you want to allow access to the Sample list.

    To restrict access to the individual items in a list, click the "Manage Permissions" item on the list's Edit menu, and then follow the same steps as would to restrict access to a list.

    Creating Lists Programmatically

    The SharePoint Object Model provides a rich set of APIs; these can be used to manipulate any SharePoint site elements, i.e., lists, documents, etc. You can create a custom list programmatically in two different ways:

    • Create the list from scratch using the Object Model
    • Create the list from an existing template

    The following code shows how to add a list item to a custom list programmatically:

    
    SPWeb spWeb = siteCollection.AllWebs["DevX"];
    SPList spList = spWeb.Lists["DevX List"];
    SPListItem spListItem = spList.Items.Add();
    spListItem["Title"] = "New List Item";
    spListItem.Update();
    

    To add a list item to a custom list with an attachment, you can use the following code:

    
    SPWeb spWeb = siteCollection.AllWebs["DevX"];
    SPList spList = spWeb.Lists["DevX List"];
    SPListItem spListItem = spList.Items.Add();
    spListItem["Title"] = "New List Item";
    SPAttachmentCollection spAttachmentCollection = item.Attachments;
    spAttachmentCollection.Add(fileName, byteArrayContents);
    spListItem.Update();
    

    In the second case, you have to have a custom list template in the template gallery. A list template is one that is used to create new list instances. It typically holds the default columns, permissions, views, content types - most of the content that a list is made up of. You can use the following code to create a list using a custom list template from the gallery:

    
    SPSite spSite = SPContext.Current.Site;
    SPWeb spWeb = spSite.OpenWeb();
    web.AllowUnsafeUpdates = true; 
    SPListTemplateCollection customListTemplates = 
       spSite.GetCustomListTemplates(spWeb); 
    SPListTemplate spListTemplate = 
       customListTemplates["DevXListTemplate"];
    spWeb.Lists.Add("Employee_Details", 
       "A custom list to store employee information", spListTemplate);
    web.Update();
    

    You can also create a list using a List Template Type available in the SPListTemplateType object:

    
    Guid listID = web.Lists.Add("DevX Custom List", 
       "This is a Custom List", SPListTemplateType.GenericList);
    SPList spList = spWeb.Lists[listID];
    

    In closing, lists are the building blocks of SharePoint applications. Some common SharePoint list types in addition to the task list you've seen demonstrated here include document libraries, task libraries and page libraries.

    123
    0 Comments  (click to add your comment)
    Comment and Contribute

     

     

     

     

     


    (Maximum characters: 1200). You have 1200 characters left.

     

     

    Networking Solutions

    Partners

    More Networking