Add ECB to View Columns

Some code to add the ECB menu to the specified columns of a view:

public void SetEcbColumnOnView(SPView view, List<string> internalNames)
{
    if (view == null)
        throw new ArgumentNullException("view");
 
    const string ListItemMenu = "ListItemMenu";
    const string ViewFields = "ViewFields";
    const string AttributeName = "Name";
 
    XElement root = XElement.Parse(view.GetViewXml());
 
    // Get all columns that use the ECB menu
    IEnumerable<XElement> fields = from el in root.Elements(ViewFields).DescendantNodes().OfType<XElement>()
                                    where el.Attribute(ListItemMenu) != null
                                    select el;
 
    // Remove the ECB Menu from all columns that currently use it.
    foreach (XElement el in fields)
    {
        el.Attributes(ListItemMenu).Remove();
    }
 
    if (internalNames.Count > 0)
    {
        //Get the field specified in the parameters
        fields = from el in root.Elements(ViewFields).DescendantNodes().OfType<XElement>()
                    let nameAttribute = el.Attribute(AttributeName)
                    where nameAttribute != null && internalNames.Any(v => v.Equals(nameAttribute.Value))
                    select el;
 
        // Add the attribute to the columns to show the ECB menu
        foreach (XElement el in fields)
        {
            Console.WriteLine(el);
            var attribute = new XAttribute(ListItemMenu, bool.TrueString);
            el.Add(attribute);
        }
    }
 
    // Update the view
    string viewXml = root.ToString(SaveOptions.DisableFormatting);
    view.SetViewXml(viewXml);
    view.Update();
}

Code should be pretty self explanatory, happy coding! :)

Performance Monitoring Class

Thought I’d share a class I use for performance monitoring to help identify areas of code that are causing bottlenecks or take a lengthy time to execute.

In SharePoint we can use the SPMonitoredScope class to wrap areas of code that we want to be monitored.

using (new SPMonitoredScope("SharePoint Performance"))
{
	// SharePoint related code
	//
}

This will output performance logging statistics to the developer dashboard (if enabled) and to the ULS.

For general SharePoint performance logging, SPMonitoredScope is ideal but it only logs methods calls to the SharePoint databases. The class I developed allows you to monitor any method, whether it’s SharePoint related or not.
Read more

Restart Services Script

Looking for a PowerShell script to restart important services across a SharePoint farm? Look no further because all you need is here :)

The script below, initiated by calling the Restart-Services function, restarts the following services on all SharePoint servers in the farm:

  • SharePoint Timer
  • World Wide Web Publishing Service
  • IIS Admin Service
  • SharePoint Administration

Read more

Alternate to CAML Or

In is a lesser known CAML operator that I’d like to bring to more peoples attention. When writing CAML and you want to restrict the results by a multi value field equaling two or more values, you’d write something like:

<where>
<or>
  </or><or>
    <eq>
      <fieldref Name="Country"></fieldref><values><value Type="LookupMulti">Scotland</value></values>
    </eq><eq>
    </eq><eq>
      <fieldref Name="Country"></fieldref><values><value Type="LookupMulti">England</value></values>
    </eq><eq>
  </eq></or>
  <or>
    <eq>
      <fieldref Name="Country"></fieldref><values><value Type="LookupMulti">Wales</value></values>
    </eq><eq>
    </eq><eq>
      <fieldref Name="Country"></fieldref><values><value Type="LookupMulti">Ireland</value></values>
    </eq><eq>
  </eq></or>
 
</where>

This will return any item that has the value Scotland, England, Wales or Ireland in the Country field. As the number of values you want to match against grows, imagine the state of the CAML query!
Read more

SharePoint 2016 On-Premises

With the next version on SharePoint due sometime during the second half of this year, it’s good to hear that Microsoft are still very much continuing to provide an on-premise version. There’s been a lot of chatter recently about this being discontinued but it’s still a kicking and a screaming. See this post from earlier today – Evolution of SharePoint; definitely worth a read.

Follow

Get every new post delivered to your Inbox

Join other followers: