none
How to set OrderedView of the list view programmatically

    Question

  • Hi

    I am trying to modify the List View of Link list in SharePoint 2010. What I need to do is to allow/disallow users to order item of Link list programmatically. We can do this easily using SharePoint browser.

    How to do this using SharePoint UI.

    Goto List->Modify Views. There is "Sort" option "Allow users to order items in this view? ".

    But I need to do this programmatically and the accessor "SPView.OrderedView" is read only so I am struck.

    I can however change the sorting columns using CAML.

    Any suggestion will be greatly appreciated.

    Thanks

    Prakash Tandukar


    tandukar
    • Moved by Mike Walsh FIN Sunday, May 22, 2011 7:02 AM "I am trying to modify the List View of Link list in SharePoint 2010." Makes me wonder why you thought a pre-SP 2010 forum was the right place for the question. (From:SharePoint - Development and Programming (pre-SharePoint 2010))
    Sunday, May 22, 2011 1:34 AM

Answers

  • Hi pkt30,

     

    I have solved it. We were going the wrong way, actually, it’s rendered via  viewflags property of  listview webpart. (Thanks to igby84's insight, http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/9b6da8da-65da-4b5d-83cb-1abb8d6a55df/)

    Here is the code snippet for your reference:

     

    using (siteCollection)

                {

     

                    using (site)

                    {

                        SPList list = site.Lists["MyLinks"];

                

                        SPLimitedWebPartManager webpartManager = site.GetLimitedWebPartManager("http://wjc:1130/Lists/MyLinks/MyView.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

                        SPLimitedWebPartCollection webparts = webpartManager.WebParts;

                        foreach (Microsoft.SharePoint.WebPartPages.WebPart webpart in webparts)

                        {

                            if (webpart.Title == "MyLinks")

                            {

                                XsltListViewWebPart xsltWebPart=(Microsoft.SharePoint.WebPartPages.XsltListViewWebPart)webpart;

                                //open option "Allow users to order items in this view? ".

     

                                if ((xsltWebPart.ViewFlags & SPViewFlags.Ordered) == 0)

                                {

                                    xsltWebPart.ViewFlags = xsltWebPart.ViewFlags | SPViewFlags.Ordered;

                                }

     

                                //close it

                                //else

                                //{

                                //    xsltWebPart.ViewFlags = xsltWebPart.ViewFlags ^ SPViewFlags.Ordered;

                                //}

                                webpartManager.SaveChanges(webpart);

                                site.Update();

                            }

                        }

     

                    }

                }

     

    Thanks,

    Porter Wang

    • Marked as answer by Porter Wang Thursday, May 26, 2011 12:40 PM
    Thursday, May 26, 2011 12:40 PM
  • Hi pkt30,

     

    For your issue, I have reproduced your issue and I make a deep research on OrderedView property of the list view. I find that it read value from view schema xml directly instead of field.

    I have tried to use the following code snippet to set schema xml but it failed.

    using (siteCollection)

                {

                    using (site)

                    {

                        site.AllowUnsafeUpdates = true;

                        SPList list = site.Lists["MyLinks"];

                        SPView view = list.Views["MyView"];

                        var xDoc = new XmlDocument();

                        xDoc.LoadXml(view.GetViewXml());

     

                        XmlElement ele = xDoc.DocumentElement;

                        ele.SetAttribute("OrderedView", "False");

                        view.SetViewXml(xDoc.OuterXml);

     

                        view.Update();                

                        site.AllowUnsafeUpdates = false;

                    }

                }

    I debug it and I find the changes will disappear after view.update(). That is to say, view.update() method can’t save the change of OrderedView attribute. And it should be saved on page directly, so you couldn’t control it via object model as far as I know.

     

    Thanks,

    Porter Wang

    • Marked as answer by KeFang Chen Wednesday, May 25, 2011 2:51 AM
    Tuesday, May 24, 2011 11:26 AM
  • Hi Porter Wang

    Thank you very much for your reply.

    I also tried to set OrderedView using reflection as below.

          public void SetOrderedView(SPView view)
            {
                Assembly testAssembly = Assembly.LoadFile(@"E:\Projects\SPS2010 DLLs\Microsoft.SharePoint.dll");

                 Type viewType = testAssembly.GetType("Microsoft.SharePoint.SPView");

                PropertyInfo orderedViewPropertyInfo = viewType.GetProperty("OrderedView");

                // get value of property
                bool value = (bool)orderedViewPropertyInfo.GetValue(view, null);

                object viewObj = (object)view;
                string value1 = (string)viewType.InvokeMember("SetAttributeValue",
                    BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic,
                    null, view, new object[] { "OrderedView", "FALSE" });

                view.Update();
            }

    But it also has no affect on the property. When I see the Microsoft SharePoint code using reflector, I found that there is no code to set the property OrderedView in the UpdateView() function of the Microsoft.SharePoint.dll.

    It seems very peculiar as we can easily set this flag using SharePoint UI but the object model does not allow.

    Thanks

     


    tandukar

    • Marked as answer by KeFang Chen Wednesday, May 25, 2011 2:52 AM
    Tuesday, May 24, 2011 12:08 PM

All replies

  • Hi pkt30,

     

    For your issue, I have reproduced your issue and I make a deep research on OrderedView property of the list view. I find that it read value from view schema xml directly instead of field.

    I have tried to use the following code snippet to set schema xml but it failed.

    using (siteCollection)

                {

                    using (site)

                    {

                        site.AllowUnsafeUpdates = true;

                        SPList list = site.Lists["MyLinks"];

                        SPView view = list.Views["MyView"];

                        var xDoc = new XmlDocument();

                        xDoc.LoadXml(view.GetViewXml());

     

                        XmlElement ele = xDoc.DocumentElement;

                        ele.SetAttribute("OrderedView", "False");

                        view.SetViewXml(xDoc.OuterXml);

     

                        view.Update();                

                        site.AllowUnsafeUpdates = false;

                    }

                }

    I debug it and I find the changes will disappear after view.update(). That is to say, view.update() method can’t save the change of OrderedView attribute. And it should be saved on page directly, so you couldn’t control it via object model as far as I know.

     

    Thanks,

    Porter Wang

    • Marked as answer by KeFang Chen Wednesday, May 25, 2011 2:51 AM
    Tuesday, May 24, 2011 11:26 AM
  • Hi Porter Wang

    Thank you very much for your reply.

    I also tried to set OrderedView using reflection as below.

          public void SetOrderedView(SPView view)
            {
                Assembly testAssembly = Assembly.LoadFile(@"E:\Projects\SPS2010 DLLs\Microsoft.SharePoint.dll");

                 Type viewType = testAssembly.GetType("Microsoft.SharePoint.SPView");

                PropertyInfo orderedViewPropertyInfo = viewType.GetProperty("OrderedView");

                // get value of property
                bool value = (bool)orderedViewPropertyInfo.GetValue(view, null);

                object viewObj = (object)view;
                string value1 = (string)viewType.InvokeMember("SetAttributeValue",
                    BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic,
                    null, view, new object[] { "OrderedView", "FALSE" });

                view.Update();
            }

    But it also has no affect on the property. When I see the Microsoft SharePoint code using reflector, I found that there is no code to set the property OrderedView in the UpdateView() function of the Microsoft.SharePoint.dll.

    It seems very peculiar as we can easily set this flag using SharePoint UI but the object model does not allow.

    Thanks

     


    tandukar

    • Marked as answer by KeFang Chen Wednesday, May 25, 2011 2:52 AM
    Tuesday, May 24, 2011 12:08 PM
  • Hi pkt30,

     

    I have solved it. We were going the wrong way, actually, it’s rendered via  viewflags property of  listview webpart. (Thanks to igby84's insight, http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/9b6da8da-65da-4b5d-83cb-1abb8d6a55df/)

    Here is the code snippet for your reference:

     

    using (siteCollection)

                {

     

                    using (site)

                    {

                        SPList list = site.Lists["MyLinks"];

                

                        SPLimitedWebPartManager webpartManager = site.GetLimitedWebPartManager("http://wjc:1130/Lists/MyLinks/MyView.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

                        SPLimitedWebPartCollection webparts = webpartManager.WebParts;

                        foreach (Microsoft.SharePoint.WebPartPages.WebPart webpart in webparts)

                        {

                            if (webpart.Title == "MyLinks")

                            {

                                XsltListViewWebPart xsltWebPart=(Microsoft.SharePoint.WebPartPages.XsltListViewWebPart)webpart;

                                //open option "Allow users to order items in this view? ".

     

                                if ((xsltWebPart.ViewFlags & SPViewFlags.Ordered) == 0)

                                {

                                    xsltWebPart.ViewFlags = xsltWebPart.ViewFlags | SPViewFlags.Ordered;

                                }

     

                                //close it

                                //else

                                //{

                                //    xsltWebPart.ViewFlags = xsltWebPart.ViewFlags ^ SPViewFlags.Ordered;

                                //}

                                webpartManager.SaveChanges(webpart);

                                site.Update();

                            }

                        }

     

                    }

                }

     

    Thanks,

    Porter Wang

    • Marked as answer by Porter Wang Thursday, May 26, 2011 12:40 PM
    Thursday, May 26, 2011 12:40 PM
  • Hi Porter Wang

    Thanks a lot. It solved the issue.

    Thanks,
    Prakash


    tandukar
    Monday, May 30, 2011 5:57 PM
  • Hi Porter Wang

    Your solution works perfectly in SharePoint 2010. I have tried the same in SharePoint 2007. Few points that I have noticed are:

    1. In SharePoint 2007, we have to use ListViewWebPart class and it does not have the "ViewFlags" accessor.

    2. Instead of the flag "ViewFlags", ListViewWebPart  has "Flags" and "ViewFlag" accessors which sound similar.

    3. They have same value when read from SharePoint View.

    The value of these two flags( "Flags" and "ViewFlag") is 4194305 when the "Allow users to order items in this view?" option is checked

    and

    the value of these two flags is 1 when the "Allow users to order items in this view?" option is unchecked.

    I also don't know meaning of this large number 4194305.

    4. So I tried to set these two flags, but there is no affect in the SharePoint UI.

    I am wondering how to do the same in SharePoint 2007.

    Thanks,
    Prakash Tandukar

     


    tandukar
    Thursday, June 02, 2011 12:22 PM