{"id":21,"date":"2008-04-09T12:27:52","date_gmt":"2008-04-09T20:27:52","guid":{"rendered":"http:\/\/sharepoint.bryanfriedman.com\/index.php\/2008\/04\/09\/con-view-sed\/"},"modified":"2008-04-09T12:27:52","modified_gmt":"2008-04-09T20:27:52","slug":"con-view-sed","status":"publish","type":"post","link":"https:\/\/archive.bryanfriedman.me\/sharepoint\/index.php\/2008\/04\/09\/con-view-sed\/","title":{"rendered":"Con-view-sed"},"content":{"rendered":"<p>You might think this would be the correct way to add a field to a list default view programmatically:<\/p>\n<blockquote>\n<p><font face=\"Courier New\" size=\"2\">string fieldInternalName = &quot;MyField&quot;;        <br \/><\/font><font face=\"Courier New\" size=\"2\">list.DefaultView.ViewFields.Add(fieldInternalName);        <br \/>list.DefaultView.Update();<\/font><\/p>\n<\/blockquote>\n<p>You&#8217;d be wrong.<\/p>\n<p>This won&#8217;t work.<\/p>\n<p>You might then try to update the list, or try to pass in the field itself to the Add method instead of the internal name.&#160; You might then try to do it with and without the Update method many times.&#160; Then you might search for code examples online where people are basically doing the same thing, only with a minor difference:<\/p>\n<blockquote><p><font face=\"Courier New\" size=\"2\">string fieldInternalName = &quot;MyField&quot;;      <br \/><\/font>    <\/p>\n<p><font face=\"Courier New\" size=\"2\">SPView defaultView = list.DefaultView;        <br \/>defaultView<\/font><font face=\"Courier New\" size=\"2\">.ViewFields.Add(fieldInternalName);        <br \/>defaultView.Update();<\/font><\/p>\n<\/blockquote>\n<p>You might say to yourself &quot;This is exactly the same thing I&#8217;m doing except I&#8217;m just using less code (and less memory).&quot;&#160; Then you might decide you&#8217;ve tried everything else so why not just try this anyway?&#160; You will then replace your code with this version.<\/p>\n<p>This will work.<\/p>\n<p>Then you might open .NET Reflector and try to make some sense of why this works one way and not the other.&#160; You will not be able to.&#160; Then you close .NET Reflector feeling even more confused.&#160; Finally you will realize you just don&#8217;t understand SharePoint or .NET as well as you thought you did.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You might think this would be the correct way to add a field to a list default view programmatically: string fieldInternalName = &quot;MyField&quot;; list.DefaultView.ViewFields.Add(fieldInternalName); list.DefaultView.Update(); You&#8217;d be wrong. This won&#8217;t work. You might then try to update the list, or try to pass in the field itself to the Add method instead of the internal [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-21","post","type-post","status-publish","format-standard","hentry","category-development"],"_links":{"self":[{"href":"https:\/\/archive.bryanfriedman.me\/sharepoint\/index.php\/wp-json\/wp\/v2\/posts\/21","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/archive.bryanfriedman.me\/sharepoint\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/archive.bryanfriedman.me\/sharepoint\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/archive.bryanfriedman.me\/sharepoint\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/archive.bryanfriedman.me\/sharepoint\/index.php\/wp-json\/wp\/v2\/comments?post=21"}],"version-history":[{"count":0,"href":"https:\/\/archive.bryanfriedman.me\/sharepoint\/index.php\/wp-json\/wp\/v2\/posts\/21\/revisions"}],"wp:attachment":[{"href":"https:\/\/archive.bryanfriedman.me\/sharepoint\/index.php\/wp-json\/wp\/v2\/media?parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/archive.bryanfriedman.me\/sharepoint\/index.php\/wp-json\/wp\/v2\/categories?post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/archive.bryanfriedman.me\/sharepoint\/index.php\/wp-json\/wp\/v2\/tags?post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}