Monday, February 13, 2012

Original title of list or library


Have you ever noticed while coding when you have changed the name of the list and then we you refer the list.title property, all you get is the new name.

Yes, that is how it should be. You must be wondering what the big deal in this is. Well, here is a catch in that.

When you write or call any method or property that returns the list items path URL or document URL, you still will get the old name of the library.

Here I have one library called Orders.

In a button click, I have written a code to change the name of a list. you can do this even from UI by going to list settings and changing the name.


protected void btnNameChange_Click(Object sender, EventArgs e)
        {
            SPWeb currentsite = SPContext.Current.Web;

            lblTitle.Text = string.Empty;

            SPList listOrders = currentsite.Lists.TryGetList("Orders");

            if (listOrders != null)
            {
                lblTitle.Text = listOrders.Title;

                listOrders.Title = "Orders New Title";

                listOrders.Update();
            }
            else
            {
                lblTitle.Text = "Orders list does not exist";               
            }
        }

Now we have a new title of the list.


Let’s click the button again.



See now we have got a message that list does not exist because we have changed the name of the list. So in this situation, what one should do if you already have code written which fetches a name of the list and perform an action on it at many places?

Are you going to rewrite your code at all the places? Think on this and you’ll get an idea how difficult this can be.

So here is a solution to this. Answer is very simple use rootfolder property of a list that always gives you original name of the list.

Let’s change a code a little.

  protected void btnNameChange_Click(Object sender, EventArgs e)
        {
            SPWeb currentsite = SPContext.Current.Web;

            lblTitle.Text = string.Empty;         

            SPListCollection lists = currentsite.Lists;

            Boolean IsNotFound = true;

            foreach (SPList list in lists)
            {
                if (list.RootFolder.Name.ToUpper() == "ORDERS")
                {
                    IsNotFound = false;

                    lblTitle.Text = "Original Title:" + list.RootFolder.Name.ToString() + " and Current Title " + list.Title;

                    list.Title = "Orders New Title";

                    list.Update();

                    break;
                }               
            }

            if (IsNotFound)
            {
                lblTitle.Text = "Orders list does not exist";  
            }          
        }

And here is what you get


Hope you must have found this useful.








1 comment:

Sudheer said...

Nice article.Thanks for sharing the info




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com