Category Archives: Script

Unhide a hidden SPField using PowerShell

Another task that’s much harder than it should be. Setting the hidden property of a SPField to false.

First you get the field itself. In this example we change the setting of the field in the library, but it could just as well be done on the site level.

$web = Get-SPWeb http://MyWeb
$list = $web.Lists["MyList"]
$field = $list.Fields.GetFieldByInternalName("MyField")

Then, when you have the actual field, you just need a bit of reflection magic.

$type = $field.GetType()
$mi = $type.GetMethod("SetFieldBoolValue",[System.Reflection.BindingFlags]$([System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Instance))
$mi.Invoke($field, @("CanToggleHidden",$true))

I don’t know why, but simply setting field.Hidden = $false doesn’t work. But the code above does!

Solution comes from here.

Making use of localized variables in javascript.

Here is a simple way of using values stored in global resource files to be used in your markup and javascript code. Simply add the following to your markup (aspx page, user control or whatever).

<script type="text/javascript">
    var myVariable = '<%= GetGlobalResourceObject("MyResourceFileName", "MyResourceKey").ToString()%>';

Once this is done, you can make use of the variable in your javascripts.

Note: These variables cannot be added in separate javascript files, but have to be added to the page directly. However, once added, they can be reached from code in the javascript file if it is imported.

EDIT: The GetGlobalResourceObject method is a standard method in the HttpContext class (link here).

Set boolean properties in powershell

I just wanted to make an existing list hidden without using SharePoint Designer, since I hate SPD. =)

So I wrote a simple powershell script to do this and quickly realized I didn’t know how to write boolean values. I tried $list.Hidden = true but it generated an error. I tried $list.Hidden = “True” but it changed nothing. A quick search on the net and I found the answer. $list.Hidden = $true worked. So boolean values are written with the dollar sign in front. Good to know. =)

So this is how to set boolean values in PowerShell:


Just in case someone wants to know. Here is the full example on how to hide a list using PowerShell.

$web = Get-SPWeb "http://url" 
$list = $web.Lists["list name"] 
$list.Hidden = $true 

Remove SharePoint feature witch name starts with… using powershell

This is how to search for features which display name starts with the given parameter (in this case it would list features which started with MyFeature).

Get-SPFeature | where { $_.DisplayName.StartsWith("MyFeature") }

EDIT: You can also find features using GUID instead with the following script:

Get-SPFeature | where { $_.Id.ToString().StartsWith("MyGUID") }

This will remove all those features which are found using the previous script.

Get-SPFeature | where { $_.DisplayName.StartsWith("MyFeature") } | foreach { Uninstall-SPFeature $_.Id -confirm:0 -force }

Sometimes (especially in SharePoint 2013), retracting wsps aren’t done properly (by SharePoint that is), and you might get errors when you are trying to reinstall a retracted wsp. After you have retracted a wsp, if the features in that wsp can still be found by Get-SPFeature in PowerShell, and/or you get an error message installing the wsp that “A feature with ID <guid> has already been installed in this farm.”, you might have to manually remove some of the resources yourself. These steps often work for me.

  1. Retract and remove wsp from server.
  2. Run powershell script above to check if the features of the wsp are still installed.
  3. If there are still features installet, delete them from the SPFarm manuall with the following command:
    $feats = (Get-SPFarm).FeatureDefinitions
    $f = $feats | where {$_.DisplayName.Contains("Feature name")} 

    This only works with single results, so make sure you get only one feature in variable $f Before deleting.

  4. Remove feature folders from the hive (for example C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\FEATURES”
  5. Remove any references in the assembly. In SP2010 (and earlier perhaps), the assembly is located at C:\Windows\assembly, and for SP2013, you can find it at C:\Windows\Microsoft.NET\assembly\GAC_MSIL.
  6. Restart the SharePoint Timer Service (Found in Services)
  7. Restart IIS (cmd “iisreset /noforce”)

Error with Timer Job in SharePoint 2010: The type ‘…’ could not be found in its specified assembly

A colleague of mine had this problem where his TimerJob wouldn’t work, and he got the error Questionmessage: “The type ‘…’ could not be found in its specified assembly”. The solution turned out to be restarting the SharePoint Timer Service after each deploy.

Run the following script in the regular command promt (cmd):

net stop sptimerv4

Wait for service to stop. Then type:

net start sptimerv4

I found the solution here: ?Timer Job in SharePoint 2010