Kategoriarkiv: 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.

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

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

Solution comes from here.

Sharepoint taxonomy

How to set a custom property of a Taxonomy term set using JSOM in SharePoint 2013

(If you are lazy and just want to see the entire script, it’s on GitHub. Don’t bother reading all these words I wrote..)

I was trying to configure a term set I created to be used for navigation, using JavaScript. The documentation on this is not extensive, so I thought I’d share it.

Defining a term set

This is done by loading the current context, getting a taxonomy session and the creating your term group and set:

For this to work you have to have the SP.Taxonomy loaded on your page. I chose to insert it in my master page, but if you can’t do that, there are other ways.

Setting the custom property

The line that actually set the custom property looks like this:

This method is buried so deep on MSDN that I originally couldn’t find it at all. It’s described on the Term class, but not, as far as I can see, on the term set class. setCustomProperty takes a property and the value of that property. A custom property can also be something that you create yourself, and in this blog post is a guide to where to find them.

Creating the term set in the term store

What you have done in the previous step is not actually creating anything in the term store. You have only created a query or task that should be executed at a later time. The actual execution is done in two steps (and this is semantically a bit unintuitive, in my opinion):

That executes your query and creates the term set in your default term store. context.executeQueryAsync takes to callbacks, the first one is executed if the query was successfull, the other if it fails.

And this is where we run the risk of ending up in callback hell. To be able to work with the objects we create, we often need to do this within the previous callback. In this cas, this is not an issue, but say we have to work with the term set, for example creating our own navigation, then this can only be done inside the callback, since this is the only place where we have access to the term set.

Creating terms with URL:s

Creating a term is as easy as creating a term set. You just use your term set and add a term to it:

Where the parameters are: name, language code and GUID. By using similar syntax as above we can also set a URL to the term:

This will create a term that leads to the root och your domain.

Putting it all together

We want to create an entire script of this! In my finished script I also delete the term set if it already exists, but it’s too long to fit here!

And there you have it! If you want to look at my complete script, it’s on GitHub.

How to create a massive amount of SharePoint sites in PowerShell

I recently wanted to create a large number of sites on my SharePoint installation and ended up writing a PowerShell script for the job that I thought I’d share. This was for a test for optimization, so I had to have quite a few in order to see some difference between tests.

First you have to find the template that’s going to be used on the sites. This is if you know the template you want to use:

But the template can also be fetched from a current site:

Then create a user that should be the owner of the site:

And then, cause I’m me and hate sites that are named test1, test2, test3 and so on, I made a list of site names:

And then you have to loop through all the names and create sites:

If you are lazy, here’s the script in its entirety.

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).

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).