Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

According to what little official documentation there is, you're supposed to use SPView.ViewData to set things such as the Explicit attribute of a ViewField, presumably by doing something like this:

    view.ViewData = @"<FieldRef Name='Field1' Explicit='TRUE'/>";

I've tried this, but it doesn't seem to have any effect at all.

It looked like there might be an alternate route, using something like this:

    XDocument viewXml = XDocument.Parse(view.GetViewXml());
    viewXml.Root.Element("ViewFields").Add(@"<FieldRef Name='Field1' Explicit='TRUE' />");
    view.SetViewXml(viewXml.ToString());
    view.Update();

But that doesn't work either. SetViewXml() seems to have no effect whatsoever.

Is there anything else I should be doing to make this work?

Thanks in advance

Josh

share|improve this question
add comment

2 Answers

I learned the trick from Stefan Stanev's blog post on ListViewWebPart & SPView - two sides of the same coin.

In a nutshell, you make a shallow copy (a clone) of the relevant view using a schema that's built as you want it to appear, and then force the clone view to be updated. That, in turn, causes the existing view to be updated.

The key piece of code looks like this:

    // load the desired schema into an XmlDocument
    XmlDocument schemaDoc = new XmlDocument();
    schemaDoc.LoadXml(desiredSchema.ToString());

   // add the existing view's ID to the schema (via the Name attribute)
   XmlElement root = doc.DocumentElement;
   root.SetAttribute("Name", existingView.ID.ToString("B").ToUpper());

   // create a clone view (to hold the desired schema)
   SPView cloneView = new SPView(<relevant SPList>, schemaDoc);

   // the magic nugget: by setting m_bExistsInDatabase=true, we force an update
   typeof(SPView).
        GetField("m_bExistsInDatabase", 
            BindingFlags.Instance | BindingFlags.NonPublic).SetValue(cloneView, true);

   // causes the existing view to be updated with the desired schema.
   cloneView.Update();

The schema is structured the same way as any view schema you can see in a Sharepoint connection in the Server Explorer window. Here's an example:

<View Type='HTML' Personal='FALSE' DisplayName='MyCustomViewEtc' DefaultView='FALSE'
     Hidden='TRUE' Scope='FilesOnly' MobileView='FALSE' TabularView='FALSE'>
  <ViewFields>
    <FieldRef Name="Title" />
    <FieldRef Name="OtherField" Explicit="TRUE" />
  </ViewFields>
  <Query>
    <OrderBy><FieldRef Name="Title" /><OrderBy>
  </Query>
  <RowLimit Paged='TRUE'>100</RowLimit>
  <Toolbar Type='None' />
</View>

Doing this requires considerable care because (as you can read in Stefan's blog post) when you create an XsltListViewWebPart, SharePoint creates a hidden companion SPView to go along with it - unless you provide the view yourself. Whether you create a view yourself or allow Sharepoint to create one for you will depend on the exact circumstances of your app.

share|improve this answer
add comment

make sure you are calling view.Update() to persist your changes.

share|improve this answer
    
I already do that, in order to save other changes. –  Josh Korn Oct 8 '11 at 11:06
    
It doesn't have any impact on the problem –  Josh Korn Oct 12 '11 at 17:53
    
I didn't see that part in your code, so I thought I would ask. At any rate, I know I have updated the view xml before, but not that particular attribute. Have you looked at the SharePoint code in reflector to see what it is doing? I have found that in some cases certain attributes are completely ignored. –  Jon Lester Oct 13 '11 at 3:12
    
Yep -- that's where the reference to m_bExistsInDatabase comes from. SPView.Update() basically skips the entire update if it thinks the view doesn't already exist in the database, hence the override. –  Josh Korn Nov 2 '11 at 20:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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