Tuesday, December 28, 2010

Check In SharePoint Documents using Powershell while Preserving "Modified" and "Modified By" MetaData

The Problem:

I was recently handed the following tasks:
  1. Checkin thousands of recently migrated documents

  2. maintain modified by metadata

  3. maintain modified date metadata

  4. must be done in powershell
The Solution:

I did some research and found a good post describing how to preserve the modifed by data here, but I still had to find a way to get it into powershell and also address the "modified date" requirement.

Below is the powershell I came up with that is working for me. You will probably want to update the list query piece to ensure the correct items are retreived. Also, I'm not quite sure the "RunWithElevatedPriveleges" is required or not, so you can test that out as well.

Hopefully this helps someone out there!

#function to return bindings to use in GetMethod() function
function Bindings()
{
return [System.Reflection.BindingFlags]::CreateInstance -bor
[System.Reflection.BindingFlags]::GetField -bor
[System.Reflection.BindingFlags]::Instance -bor
[System.Reflection.BindingFlags]::NonPublic
}

#Function to checkin a file using a specific user
function CheckInFileByUser([Microsoft.SharePoint.SPFile]$file, [string]$comment, [Microsoft.SharePoint.SPCheckinType]$checkInType, [Microsoft.SharePoint.SPUser]$user)
{
$bindings = Bindings
[System.Type[]] $types = [string],[Microsoft.SharePoint.SPCheckinType],[bool], [Microsoft.SharePoint.SPUser]
$method = $file.GetType().GetMethod("CheckIn",$bindings,$null,$types,$null)
$params = $comment,$checkInType,$false,$user
$method.Invoke($file,$params)
}

#Main code section
$baseUrl = "http://localhost/sites/records"
$listName = "Record Library"

Clear-Host
$site = Get-SPSite $baseUrl
$web = $site.OpenWeb($site.RootWeb.ID)
$list = $web.Lists[$listName]

#example only, get items using view or query of your choice
Foreach($item in $list.Items)
{
$file = $item.File
if($file.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
{
#get the current date to use later
[System.DateTime]$date = $item["Modified"]
[Microsoft.SharePoint.SPFile]$file = $item.File

#get the current editor SPUser to use when checking in the file
$user = New-Object microsoft.SharePoint.SPFieldUserValue($web, $item["Editor"])

#check in the file
CheckInFileByUser $file "My Comment" "MajorCheckIn" $user.User
#change the modified date back to the original value
[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(
{
$item = $list.Items.GetItemById($item.ID);
$item["Modified"] = $date
$item.Update()
} )
}
}

Thursday, October 21, 2010

Disable Ribbon Positioning System in SharePoint 2010

Ok, so I'm working on branding a SharePoint 2010 public facing internet site, and I just couldn't figure out how to disable the ribbon positioning system.

I started out using the steps provided in this article (I recommend reading this article if you are unsure of how the v4master page works with the ribbon positioning system), which are as follows:

1. Remove the "_fV4UI" variable from the top of the page
2.Remove or change the Workspace element's ID to make the ECMA Script code abord early during window resizes
3.Remove the "scroll=no" in the body tag
4. In your CSS file, remove or change the width, height, and overflow declarations ont he bady tag
5. Optionally, in your CSS file, remove the s-4 workspace and s4-bodycontainer rules since they are no longer necessary

So after going through these steps, the scrolling still did not work for me. I'm guessing I missed a step that was inferred in step 5, but here is what I added to my custom CSS file to get scrolling to work:

body.v4master{
height:100%;
width:100%;
overflow:auto;
}

Hopefully this helps someone out there!

Errors when Starting SharePoint User Code Service (aka Sandboxed Code) with Powershell

The Short:
The service account used to host the SharePoint User Code Service needs to be added to the Performance monitors group on the server running the service.

The Story:
Recently, I was working on installing some SharePoint 2010 Farms using powershell, and I kept seeing errors when starting the SharePoint User Code (aka Sandboxed Code) Service. After installing the farm, the SharePoint 2010 User Code Host windows service was stopped, and the following errors appeared in the event log:


- "The SharePoint 2010 User Code Host service terminated unexpectedly"

After looking through the ULS logs on the server, I noticed errors around calls to the performance counters on the machine. It turns out that the user account that is used for the User Code Service needs additional permissions on the machine running the service. Specifically, the account to be added to the Performance Monitor Users group.

The issue only appears to be an issue when starting the service from powershell specifically because when started from central administration, the user account defaults to the Farm account, and that account is automatically added to the correct group.

More error details can be found at http://support.microsoft.com/kb/983081/en-US, but the kb article does not specify the group required.

I hope this helps someone out there!