Month: November 2016

Hiding Group Headers from SharePoint List View By jQuery

When I create a list view web part with grouping by some field, the groups will be show as this format “{FieldName}:{value of a group}({number of elements in group})”. However I just want to show “value of the current group” as a group header.

2016-11-17_1705
Group headers example

These following are what I did to hide them


$("td .ms-gb").each(function() {
var target = $(this)[0];
if (target.childNodes.length >= 3)
{
$(target.childNodes[0]).html('');
$(target.childNodes[1]).replaceWith(target.childNodes[1].nodeValue.replace(':', ''));
$(target.childNodes[2]).html('');
}
});

In this:

  • ms-gb: default class used for containing groups headers
  • childNodes[0]: <a> tag with text is {FieldName}
  • childNodes[1]: “:{value of a group}”
  • childNodes[2]: <span> tag with text is ({number of elements in group})

Enjoy!

SharePoint – Inherits in content type

Somewhen, I need to create a content type inherited from a custom content type (or OOTB content type). However, I want to remove some field from parent content type. After working hours, I realized that there is a property inherits in content type definition is defined as:

Optional Boolean. The value of this attribute determines whether the content type inherits fields from its parent content type when it is created.

If Inherits is TRUE, the child content type inherits all fields that are in the parent, including fields that users have added.

If Inherits is FALSE or absent and the parent content type is a built-in type, the child content type inherits only the fields that were in the parent content type when SharePoint Foundation was installed. The child content type does not have any fields that users have added to the parent content type.

If Inherits is FALSE or absent and the parent content type was provisioned by a sandboxed solution, the child does not inherit any fields from the parent.

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

So, to remove the Title field, for example, using the RemoveFieldRef , you must also update Inherits=”FALSE” and I always include Overwrite=”TRUE“. Like these following:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <ContentType ID="0x010100C568DB52D9D0A14D9B2FDCC96666E9F20079481"
Name="My Content type"
Group="Custom Content types"
Inherits="FALSE"
Overwrite="TRUE"
Version="0">
<FieldRefs>
<RemoveFieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" />
</FieldRefs>
</ContentType>
</Elements>

Hope it’s helful.

 

Sharepoint – Propagate site column to list.

After searching time, I find a great article about propagating updates in site content type to child contents. In this, he said “When you use a site collection content type on a list, what actually happens is a “copy” of the original content type is made and stored in the list. Thus, any changes to the original content type is not propagated into the list”. As MS explains it:

“Do not, under any circumstances, update the content type definition file for a content type after you have installed and activated that content type. Windows SharePoint Services does not track all the changes made to the content type definition file. Therefore, you have no reliable method for pushing down all the changes made to site content types to the child content types.”

More on the article can read here: https://johanleino.wordpress.com/2009/08/11/propagating-updates-to-content-types/

His solution is used c#. In my case I need to process by powershell. Here is my solution:


param
(
 [string]$SiteUrl
)

if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
 Add-PsSnapin Microsoft.SharePoint.PowerShell
}

function PropagateFieldToList()
{
 if($list.Fields.ContainsField($fieldOnWeb.InternalName) -eq $true)
 {
 $fieldInList = $list.Fields[$fieldOnWeb.Id]
 $fieldInList.SchemaXml = $fieldOnWeb.SchemaXml
 $fieldInList.Update()
 Write-Host "Propagate field $field to Page library on $web."
 }
 else
 {
 $list.Fields.Add($fieldOnWeb)
 $list.Update()
 Write-Host "Add field '$($fieldOnWeb)' to page list."
 }

 AddColumnToContentType "ContentTypeName" $fieldOnWeb $false
}

function AddColumnToContentType($ctype, [Microsoft.SharePoint.SPField] $field, $isRequireField) {
 if ($ctype.FieldLinks[$field.InternalName] -eq $null) {
 $fieldLink = New-Object Microsoft.SharePoint.SPFieldLink($field)
 $fieldLink.Required = $isRequireField;
 $ctype.FieldLinks.Add($fieldLink);
 $ctype.Update()
 Write-Host "Add field $field to content type $($ctype.Name)."
 }
}

try
{
 $site = get-SPSite $SiteUrl
 $rootweb = $site.RootWeb
 $fieldOnWeb = $rootweb.Fields.GetFieldByInternalName('FieldInternalName')
 if($fieldOnWeb -ne $null)
 {
 foreach ($web in $rootweb.Webs)
 {
 $pageLibName = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPagesListName($web)
 $list = $web.Lists[$pageLibName]
 PropagateFieldToList
 }
 }
}
finally
{
 if ($site -ne $null)
 {
 $site.Dispose()
 }
}

Hope