Article
8 comments

Dynamically display department information for a user in a list

Over the last couple of years a couple of times I heard a request by my customer. Is it possible to show the department information of a user who created an item in lists in a dynamic way? My answer to this was always, that it is not supported and cannot be done. The first time I heard this request was back in the days of Microsoft SharePoint Server 2007. This can only be accomplished by using an event receiver on the list. This will help with the initial filling of the information, but when a name of a department the user is in changes all list item needs to be changed too.

In SharePoint 2010 there is a more confident way than to use an event receiver and the solution is called Dependant Lookup fields. That field can be used for every lookup field.

The Basics

The “Author” or “Created by” field from an end-user perspective is a person field but from a technical point of view it is just a simple lookup field to the so-called “User Information List”. This list in SharePoint is stored in every site collection at root level and has every user provisioned to a site collection. The values, such as, of the “Created by” will be stored in the following format.

1;#spserver\StefanBauer

<ID of item in user information list>;#<user account>

The same way as a normal lookup does. This lookup field behavior of the people field can be used to add another dependent lookup field to a site collection.

Get additional fields for users to the list

In this use case I created lookup for the department information that is stored with every user in the “User Information List”. I wanted to create a dependant lookup field called “Authors Department”. For the deployment of this field I used a simple feature event receiver. The practical reason for this is that dependent lookup fields or cross web lookup fields can be provisioned more easily by using code that the declarative way using xml definitions. The code for this is short and simple.

[code lang=”csharp”]string authorDepartmentCol = web.Fields.AddDependentLookup("Authors Department", web.Fields[SPBuiltInFieldId.Author].Id);
SPFieldLookup authorDepartment = (SPFieldLookup)web.Fields.GetFieldByInternalName(authorDepartmentCol);
authorDepartment.Description = "Can be used to add the department information of an author or created by to a list";
authorDepartment.LookupField = web.Fields[SPBuiltInFieldId.Department].InternalName;
authorDepartment.Update();
web.Update();
[/code]

The field will deploy correctly in the site collection but, and this is the first limitation, the field cannot be found in the site columns. The field is there but somehow SharePoint filter dependant lookup fields out. To use this field it must be added to a site content type or a specific list using code too. In my site collection I created at root level a list called “Author with Department” and the new “Author Department” field will be added to this list. The following code will do that:

[code lang=”csharp”]
SPList list = web.Lists.TryGetList("Author with Department");
if (list != null) {
if (list.Fields.TryGetFieldByStaticName(web.Fields["Authors Department"].StaticName) == null)
{
list.Fields.Add(web.Fields["Authors Department"]);
list.Update();
}
}[/code]

After the deployment of the solution and once it the feature has been activated it is time for a little test run.

The department field in action

The following short video will show the dependant field in action with two different users.

[vimeo http://vimeo.com/35774443]

As promised the department information will be added automatically for the different users. There is no other functionality involved or feature event receiver. The department information will be also viewed to users that have only read permission in the portal.

Conclusion

Is it possible to add additional information for a person field to a list? Yes. Is it supported by Microsoft? Maybe. I searched MSDN and haven’t found any reason that this is not supported but I think it needs to be used carefully and wisely. As any other lookup or dependant lookup field can cause impact on the performance of a list this should be planned carefully too.

I think this example for the use of dependant lookups will give a nice outlook how the next SharePoint Server might be enhanced. In the past department information needs to be added manually by every user, which can also cause wrong information. And the information won’t be updated if the user moves to a different department. It also takes longer for the user to enter all required Information.

This use cases for this kind of dependant lookup fields to the “User Information Lists” are nearly endless. This could be used in help desk systems, for task tracking or for the use in a content query web part. The information of the user will be update automatically updated by the profile synchronization.

Download Dynamic Department Field on List VS Solution

8 Comments

  1. excellent information. I really hope we manage to do it on our installation

    Reply

  2. Hello Stefan,

    We try to activate the feature but we get an error. In the log we find “Field with name “Authors Department” was not found”

    Please advise.

    Kind regards,
    Mario

    Reply

    • Hi Mario,

      sorry had a little problem in the solution and fixed it. Can you please download it again. You should also make sure that the “Author with Department” List exists

      Kind regards
      Stefan

      Reply

      • Hello Stefan,

        That worked thanks.
        Question:
        How can we make this column available for other lists?
        Could we make it a site column?

        And if above is not possible: we would like to add it to a list on a subweb: Can you tell me how?

        Kind regards,
        Mario

        Reply

        • The code already creates a site column but it is in the _hidden group. You will find this field in the root of your site collection when you append “_layouts/fldedit.aspx?field=Authors_x0020_Department”.
          There you can also change the group.

          If you really like to use it in a productive environment I recommend you to let have a developer a look over my code. He/She will also be able to create the fields with the correct names. It can also extended to other fields too.

          /greetings
          Stefan

          Reply

          • Hello Stefan,

            Thanks.
            How we did it now:
            We used Powershell to add it to a Content Type.
            When we add the Content Type to a list we get the Column available.

            Good enough for now and we will let a developer look into this.

            Thanks again.

            Kind regards,
            Mario

    • I haven’t tested it but it should. In this case SharePoint haven’t changed.

      /Stefan

      Reply

Leave a Reply

Required fields are marked *.


This site uses Akismet to reduce spam. Learn how your comment data is processed.