I am new to SharePoint dev. Is there some way to change or filter (output) data in a SharePoint library from code.

I tried to use CAML query and set query to defaultView and update SPList but with no result. Then I tried to create a custom view and add it to the view collection (it works) but I don't know how set current view.

Are there some possibilities how to handle it? Basically I need set up a filter or CAML query from my visual web part for a document library.


When you add the new view, you have the possibility of marking it as default view:

public SPView Add(
    string strViewName,
    StringCollection strCollViewFields,
    string strQuery,
    uint iRowLimit,
    bool bPaged,
    bool bMakeViewDefault,
    SPViewCollection.SPViewType type,
    bool bPersonalView

from MSDN.

As you can see, by setting bMakeViewDefault to true, your new view will be used as the default view for the list or library.

If you already have created the view, you can mark it as default view with this code:

SPView yourView = CurrentList.Views["Name or ID of view"];
yourView.DefaultView = true;

as found here

| improve this answer | |

Note that Views are quite static things in SharePoint. If you want to query the document library dynamically from your web part then you may consider using the KeywordQuery class. This uses SharePoint Search to find the matching documents but you need to display the results yourself. Also you will need to filter the results to your document library.

Here is a good description how to do it: http://msdn.microsoft.com/en-us/library/office/ms551453(v=office.14).aspx

And here is a complete sample. http://msdn.microsoft.com/en-us/library/office/ms493601(v=office.14).aspx

| improve this answer | |

I think problem is that view does not refresh on page

    SPSite siteColl = SPContext.Current.Site;
        SPWeb site = SPContext.Current.Web;
            using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID))
                using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(site.ID))

                    ElevatedsiteColl.AllowUnsafeUpdates = true;
                    ElevatedSite.AllowUnsafeUpdates = true;

                    SPList oList = ElevatedSite.Lists["share"];
                    SPView oView = oList.DefaultView;
                    StringCollection columns = oView.ViewFields.ToStringCollection();

                    SPQuery oQuery = new SPQuery();

                    var docquery = new XElement("Where",
                                  new XElement("Eq",
                                      new XElement("FieldRef", new XAttribute("Name", "GUID")),
                                      new XElement("Value", new XAttribute("Type", "Text"), "1A")

                    oQuery.Query = docquery;

                    oList.Views.Add("pohled", columns, oQuery.Query, 30, true, true);
                    oView.DefaultView = true;

                    ElevatedsiteColl.AllowUnsafeUpdates = false;
                    ElevatedSite.AllowUnsafeUpdates = false;

| improve this answer | |
  • Is this an answer or an edit? Right here seems to be strange code, you add a new view, but marks the old default view as default view again: oList.Views.Add("pohled", columns, oQuery.Query, 30, true, true); oView.DefaultView = true; oView.Update(); – Robert Lindgren Aug 6 '14 at 9:08

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.