Monday, February 28, 2011

Display Created and modified details of a list item in share point through coding.

In share point the list item will store the details of the created and modified details of the user by using the user credentials details. It will store as "number#user name" format.here the number is a system defined key to get the user details while using the people picker. So we need to eliminate the number and display only the user name. The details are stored internally created By and Edited By tags, we will get the details by getting the details.
//get the created and modified details of the list item.

        private static void ShowCreatedByAndModifiedByDetailsOftheListItem(SPList list)
        {
            string createdBy = string.Empty;
            string modifiedBy = string.Empty;
            int index;
            foreach (SPListItem item in list.Items)
            {

                createdBy = item["Created By"].ToString();
                if (createdBy != null)
                {
                    index = createdBy.IndexOf('#');
                    if (index > 0)
                    {
                        Console.WriteLine(item.Title + " created by " + createdBy.Substring(index + 1, createdBy.Length - index - 1));
                    }
                    else
                    {
                        Console.WriteLine("index is less than 1,there is an error in the created by name");
                    }
                }
                modifiedBy = item["modified By"].ToString();
                if (modifiedBy != null)
                {
                    index = modifiedBy.IndexOf('#');
                    if (index > 0)
                    {
                        Console.WriteLine(item.Title + " modified by " + modifiedBy.Substring(index + 1, modifiedBy.Length - index - 1));
                    }
                    else
                    {
                        Console.WriteLine("index is less than 1,there is an error in the modified by name");
                    }
                }
            }
        }


As the above code we need to eliminate the key from the name. Thus we will get the names of the particulars. We will the string of the user name with the key value in the line createdBy = item["Created By"].ToString();. Print the string after the # value. Apply same thing for modified By details also.

Hope this will help you.

Share this