Change Content Type With PowerShell

Objective:

Change the content type of many files in a single library.

Solution:

Use the following PowerShell script to change the content type instead of editing each file one at a time. In my example, I change the content type from “Document” to “Process”.


function Reset-SPFileContentType ()
{
#Get web, list and content type objects
$web = Get-SPWeb -Identity http://spsite
$list = $web.Lists["Documents"]
$oldCT = $list.ContentTypes["Document"]
$newCT = $list.ContentTypes["Process"]
$newCTID = $newCT.ID

#Check if the values specified for the content types actually exist on the list
if (($oldCT -ne $null) -and ($newCT -ne $null))
{
#Go through each item in the list
$list.Items | ForEach-Object {
#Check if the item content type currently equals the old content type specified
if ($_.ContentType.Name -eq $oldCT.Name)
{
#Check the check out status of the file
if ($_.File.CheckOutType -eq "None")
{
#Change the content type association for the item
$_.File.CheckOut()
write-host "Resetting content type for file" $_.Name "from" $oldCT.Name "to" $newCT.Name
$_["ContentTypeId"] = $newCTID
$_.Update()
$_.File.CheckIn("Content type changed to " + $newCT.Name, 1)
}
else
{
write-host "File" $_.Name "is checked out to" $_.File.CheckedOutByUser.ToString() "and cannot be modified"
}
}
else
{
write-host "File" $_.Name "is associated with the content type" $_.ContentType.Name "and shall not be modified"
}
}
}
else
{
write-host "One of the content types specified has not been attached to the list"$list.Title
}
$web.Dispose()
}
Reset-SPFileContentType

Credit to Phil Childs’ article for SharePoint 2010. The script also works for SP 2013. 

Workflow to Update Hyperlink Display Text

Problem:

I wanted to use Quick Edit to update the URL column for a bunch of items in a list. The problem is that when you edit a hyperlink column, you have to click the little hyperlink editor icon next to the cell to also edit the display text. Otherwise, it just shows up as the URL.

Solution:

Use a workflow to change the display text to something else.

1. First, create a new text column or use an existing text column
2. Open SharePoint Designer
3. Create a new List Workflow
4. Add Update List Item action
5. Click “Add…”

6. Scroll through and find your new column. In this example I used the existing Description column.

7. Click “…” to open the String Builder

8. Click “Add or Change Lookup”
9. Select the hyperlink column
10. Change “Return field as:” to URL

11. Click “OK”
12. IMPORTANT: Add a comma and a space.
13. Click “Add or Change Lookup” again to select a column, or type out what you want your display text to be. I am setting mine to be the Title of the item.
14. Click “OK” > “OK” > “OK”
15. Add another Update List Item action
16. Click “Add…”
17. Set the hyperlink column to the value of the column we updated in the previous step

18. Click “OK” > “OK”
19. Change the Start Options to “Start workflow automatically when an item is changed”
20. Publish

Now you can Quick Edit a bunch of URLs without having to manually change the display text.

Search Crawl Log Error – Access is denied

Scenario:

Crawl History shows 0 successes and a bunch of security errors.

The following error is logged in the Error Breakdown:

“Access is denied. Verify that either the Default Content Access Account has access to this repository, or add a crawl rule to crawl this repository.”

Possible Solution:

Check your hosts file. In my case, host headers had been commented out for testing and never changed back.

 

Workflow Stuck On Starting After Password Change

I generally use my own account to create and publish workflows. One day, none of my workflows were completing and they were all stuck on “starting”. I thought maybe there was something wrong with the workflows themselves so I tweaked one of them a bit and re-published and voila! It worked! But the thing I tweaked really shouldn’t have “fixed” anything. So I changed it back and again, it worked!

After banging my head against my desk for a little while I remembered updating my password that morning. Maybe all I needed to do was republish my workflows.

Which is what I did and it fixed everything.

There may have been an easier solution, but this worked for me.

Summary:

If you’re going to use your own account to create and publish workflows, you might have to re-publish every time you change your password.

You should probably use an admin account without a password policy to publish workflows.

 

Keep Results Empty Until User Searches

Scenario:

When you navigate to a custom search page, the Search Results web part is already populated even though you haven’t searched for anything yet.

You want the Search Results web part to remain empty until a user searches for something.

Solution:

Wrap the query template. {?{searchboxquery}}

1. Edit the page
2. Edit the Search Results web part
3. Click “Change query”

4. In the Query text box, wrap the filters {?{searchboxquery}}

5. When you click Test query, the preview should say “This query returned no results.”
6. Click OK to save the query
7. Click OK to save and close the web part properties
8. Save the page

Copy Missing Objects From One Content Database to Another

I recently applied a cumulative update to a SharePoint environment that hadn’t been patched in years. When I tried to upgrade the main production content database, the process failed and rendered the site inaccessible. The errors logs mentioned invalid objects such as “Webs” and “Sites”, and upon further troubleshooting we learned that because of permission issues, the content database was missing critical views and procedures due to one of the updates rolled up into the cumulative update.

NOTE: I’d like to take a moment to stress the importance of backups. CONFIRM THAT YOU HAVE A HEALTHY BACKUP BEFORE PERFORMING MAINTENANCE. EVERY. SINGLE. TIME! Thank you.

Even though we corrected the permission issues, no matter what we tried we could not re-run the upgrade to fix the problem. Instead, we had to copy those missing objects and views from the sandbox content database to prod, and cross our fingers it would work. (spoiler: it did)

  1. Right click your healthy database > Tasks > Generate Scripts…
  2. In the Choose Objects prompt, select the objects you would like to copy. In our case we copied Views and Stored Procedures.
  3. Click Next
  4. Select “Save to new query window”
  5. Next > Next > Finish
  6. Change the database in the query to the database that’s missing objects
  7. Execute
  8. Browse the site to confirm content
  9. Drink