{"id":1800,"date":"2015-11-13T10:57:36","date_gmt":"2015-11-13T10:57:36","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=1800"},"modified":"2020-03-25T16:31:58","modified_gmt":"2020-03-25T16:31:58","slug":"add-ecb-to-view-columns","status":"publish","type":"post","link":"https:\/\/www.stuartroberts.net\/index.php\/2015\/11\/13\/add-ecb-to-view-columns\/","title":{"rendered":"Add ECB to View Columns"},"content":{"rendered":"<p>Some code to add the ECB menu to the specified columns of a view:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic void SetEcbColumnOnView(SPView view, List internalNames)\n{\n    if (view == null)\n        throw new ArgumentNullException(&quot;view&quot;);\n\n    const string ListItemMenu = &quot;ListItemMenu&quot;;\n    const string ViewFields = &quot;ViewFields&quot;;\n    const string AttributeName = &quot;Name&quot;;\n\n    XElement root = XElement.Parse(view.GetViewXml());\n\n    \/\/ Get all columns that use the ECB menu\n    IEnumerable fields = from el in root.Elements(ViewFields).DescendantNodes().OfType()\n                                    where el.Attribute(ListItemMenu) != null\n                                    select el;\n\n    \/\/ Remove the ECB Menu from all columns that currently use it.\n    foreach (XElement el in fields)\n    {\n        el.Attributes(ListItemMenu).Remove();\n    }\n\n    if (internalNames.Count &gt; 0)\n    {\n        \/\/Get the field specified in the parameters\n        fields = from el in root.Elements(ViewFields).DescendantNodes().OfType()\n                    let nameAttribute = el.Attribute(AttributeName)\n                    where nameAttribute != null &amp;&amp; internalNames.Any(v =&gt; v.Equals(nameAttribute.Value))\n                    select el;\n\n        \/\/ Add the attribute to the columns to show the ECB menu\n        foreach (XElement el in fields)\n        {\n            Console.WriteLine(el);\n            var attribute = new XAttribute(ListItemMenu, bool.TrueString);\n            el.Add(attribute);\n        }\n    }\n\n    \/\/ Update the view\n    string viewXml = root.ToString(SaveOptions.DisableFormatting);\n    view.SetViewXml(viewXml);\n    view.Update();\n}\n<\/pre><\/div>\n\n\n<p>Code should be pretty self explanatory, happy coding! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some code to add the ECB menu to the specified columns of a view: Code should be pretty self explanatory, happy coding! \ud83d\ude42<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[3],"tags":[17,81],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-t2","_links":{"self":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1800"}],"collection":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=1800"}],"version-history":[{"count":2,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1800\/revisions"}],"predecessor-version":[{"id":1872,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1800\/revisions\/1872"}],"wp:attachment":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=1800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=1800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=1800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}