PowerShell to Embed JavaScript using Site Collection User Custom Action

The script below will insert a JavaScript reference that will be embedded into all pages of your site collection.

Assuming you have a JavaScript file called global.js in the Site Collection’s Site Assets library. Can be parameterised for more customisation.

Continue reading

Advertisements

SharePoint Search Expired Claims Cookie

We had an issue with a client where their SharePoint search crawls were periodically crawling successfully, but then on other subsequent crawls show a lot of warnings, saying “The claims cookie used by the crawler has expired.”

Expired Claims Cookie

For this web application, we had the Default zone set up with Windows Authentication for the search crawler, and an ADFS Trusted Identity Provider for users to log in through a custom login page.

The client also had a requirement where logins have to expire after an hour, so the CookieLifetime setting in the Security Token Service Config for the farm had been modified to 1 hour from 5 days.

Continue reading

PowerShell to Insert Field to List View at Specific Location

Added a new field (AddressLine1) to a list, and needed to display the field in all list views. It needed to always display before another field (AddressLine2).

Script below loops through all views of a list, and if the AddressLine2 field exists, it will add AddressLine1 just before it.

Note: Can’t use ForEach loop to go through each list view as the .Update() will throw an error, due to restrictions in updating collection items while enumerating.

Continue reading

Reordering SharePoint Fields using PowerShell

The order in which fields are displayed in the New, Edit, and Display forms for a list item is determined by the FieldLinks order of its content type.

https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldlinkcollection_members(v=office.15).aspx

The SPFieldLinkCollection has a handy Reorder method that we can use to set the order of the fields, using their internal names

The script below does this through PowerShell:

Continue reading

Handy CAML Query Snippets

Getting Checked out Files

<Where>
  <IsNotNull>
    <FieldRef Name='CheckoutUser' />
  </IsNotNull>
</Where>

or

<Where>
  <Eq>
    <FieldRef Name='_Level' />
    <Value Type='Integer'>255</Value>
  </Eq>
</Where>

Getting Published Files

<Where>
  <Eq>
    <FieldRef Name='_Level' />
    <Value Type='Integer'>1</Value>
  </Eq>
</Where>

Getting Checked-in Files

<Where>
  <Eq>
    <FieldRef Name='_Level' />
    <Value Type='Integer'>2</Value>
  </Eq>
</Where>

Getting Files Only (i.e. excluding Folders)

<Where>
  <Eq>
    <FieldRef Name='FSObjType' />
    <Value Type='Integer'>0</Value>
  </Eq>
</Where>

Getting Folders Only (including Document Sets)

<Where>
  <Eq>
    <FieldRef Name='FSObjType' />
    <Value Type='Integer'>1</Value>
  </Eq>
</Where>

Comparing URLs

<Where>
  <Eq>
    <FieldRef Name='WorkflowLink' />
    <Value Type='URL'>/subsite/listname/item-url.docx</Value>
  </Eq>
</Where>

Note that you must use a server relative URL when querying against a URL field.


Querying Content Type ID

<Where>
  <Eq>
    <FieldRef Name='ContentTypeId' />
    <Value Type='ContentTypeId'>0x0120D52000058D5968BC2445489FC509B9E038862900A88F92769A306640872EDF8D34E64427</Value>
  </Eq>
</Where>

Querying Current User

<Where>
  <Eq>
    <FieldRef Name="UserField"></FieldRef>
    <Value Type="User"><UserID /></Value>
  </Eq>
</Where>

or

<Where>
  <Eq>
    <FieldRef Name="UserField"></FieldRef>
    <Value Type="Integer"><UserID /></Value>
  </Eq>
</Where>

Querying User Based on ID

<Where>
  <Eq>
    <FieldRef Name="UserField" LookupId="TRUE"></FieldRef>
    <Value Type="User">11</Value>
  </Eq>
</Where>

or

<Where>
  <Eq>
    <FieldRef Name="UserField" LookupId="TRUE"></FieldRef>
    <Value Type="Integer">11</Value>
  </Eq>
</Where>