0

I want to order the view of the document library in the descending order it was created.

My code for doing such is:

static void Main(string[] args)
{
    string webApp = "http://bipc11:1234/sites/";
    string subSite = "bestpractices";
    string url = webApp+subSite;
    using (SPSite siteCollection = new SPSite(@"http://bipc11:1234/sites/bestpractices/"))
    {
        using (SPWeb web = siteCollection.RootWeb)
        {
            if (web != null)
            {
                SPListCollection docLibColl = web.GetListsOfType(SPBaseType.DocumentLibrary);
                foreach (SPList list in docLibColl)
                {
                    modifyView(list.Title,web);                             

                }
            }

            Console.WriteLine("Success");
        }

    }
 }
 static void modifyView(string title,SPWeb web)
 {
    SPList list = web.Lists[title];
    Console.WriteLine(list.Title);
    SPView view = list.DefaultView;
    view.Query = @"<OrderBy><FieldRef Name='Modified' Ascending='FALSE'/></OrderBy>";
    view.Update();  
 }

I am getting the error at line view.Query that the object reference not set to an instance of an object.

What should I do?
Please help.

0

You are looping through all Document Libraries in the Site collection. The above code returns one List name Wfpub for which default view is null, hence view.Query gives the exception. You should only retrieve that library which you want to change. Also do't dispose RootWeb, it is automatically disposed.

  • I want all library views to be updated.. @Nadeem Yousuf – users1100 Aug 16 '13 at 6:20
  • Then put the code in modifyView method in try catch block, so that the code will not fail for lists like wfpub whose List.DefaultView is null. – Nadeem Yousuf Aug 16 '13 at 6:25
  • One more help I need from you is that I am working on publishing site and I want to make these changes to purely document libraries only not style libraries or Master Page libraries. Is there any way to get the list of libraries that are purely document libraries? – users1100 Aug 16 '13 at 6:36
  • There is no easy way of doing this. You can use a check if (list.BaseTemplate == SPListTemplateType.DocumentLibrary). It will limit libraries like List Template gallery, Master page Libraries. However, Style libraries etc will still appear. You may need some extra logic to limit them in the result. – Nadeem Yousuf Aug 16 '13 at 6:57
  • Thanks for reply.. I will check it in "Switch" to bypass them. – users1100 Aug 16 '13 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.