Today I had to working on a situation where we need to replace a web part with a different one.
First step is to determine what pages have this web part on them. In our environment the web part was only added to the home page of a site. So the script assumes this is the only location of this particular web part
Here is the powershell script I used:
NOTE: I found a bug in that it may return ErrorWebPart. This is due to the httpcontext not being set.
I have modified the code accordingly(reference: http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/607f3980-53c0-4007-bb34-9969ed6ea4fc)
Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
# web application to crawl through
$webApp = "http://webapplication"
# output file for later reviewal
$outputFile = "C:\outputFile.txt"
# webpart full name - not the title
$webPartFQN = "assembly.webpart.name"
write-host "Reading from webapp $($webApp)"
$site = Get-SPWebApplication $webApp | Get-SPSite -limit All
# loop through all the site collection in the web application
foreach($s in $site)
{
#loop through all the webs in the site collection
foreach($web in $s.AllWebs)
{
if ($null -eq [System.Web.HttpContext]::Current){
$isContextNull = $true;
$sw = New-Object System.IO.StringWriter;
$resp = New-Object System.Web.HttpResponse $sw;
$req = New-Object System.Web.HttpRequest "", $web.Url, "";
$htc = New-Object System.Web.HttpContext $req, $resp;
#explicitly cast $web to spweb object else sharepoint will #see it as a PSObject $htc.Items["HttpHandlerSPWeb"] = $web -as [Microsoft.SharePoint.SPweb];
[System.Web.HttpContext]::Current = $htc;
if ($sw -ne $null){
$sw.Dispose()
}
} else {
Write-Host "HttpContext already set";
}
Try{
# get the welcome page in the web's rootfolder.
$defPage = $web.RootFolder.WelcomePage
if( $defPage -eq $null -or $defPage -eq "")
{
#didn't find a welcome page, loop through all pages in rootfolder then
foreach($f in $web.RootFolder.Files)
{
CheckForWP( "$($web.url)/$($f.Name)")
}
}
else
{
#process page to check if the webpart exists
CheckForWP( "$($web.url)/$($defPage)")
}
}Catch{
[Exception]
write-host "Exception thrown while opening web{$($web)} and web part manager."
write-host $_
} }
}
function CheckForWP($page)
{
$pageUrl = $page
# Get the webpart manager to find all the webparts on the page.
$webpartmanager=$web.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
if( $webpartmanager -ne $null)
{
#loop through the web parts
foreach($wp in $webpartmanager.webparts)
{
# get the title and name of the webpart
$wpt = $wp.Title
$wpn = $wp.ToString()
if($wpn.ToLower() -eq $webPartFQN)
{
#write out that I found the webpart in question and the webpart names
write-host "Found on: $($pageUrl)"
$pageUrl | Out-File $outputFile -Append
" $($wpt)" | Out-File $outputFile -Append
" $($wpn)" | Out-File $outputFile -Append
}
}
} else {
write-host "failed to get webartmanager"
}
}
Pocket Fluff - SharePoint Development
Thursday, May 23, 2013
Sunday, May 19, 2013
Library/Lists enforcing versioning settings
So one thing that came up at one of the talks at 2013 Best Practices Conference for SharePoint was how to enforce or set the library/list version settings. The speaker was asking on if there was an easy way to set defaults to these settings, preferably without code.
I know this is a pain point, because I have admin a farm that had issues with versioning that is out of control. (ex. 15GB library with one document taking up 7GB of space. The file was only 15MB in size.)
Now there are a few ways to accomplishing setting the default values without coding, but there will be limitations. For instance, SharePoint has no control of how many draft versions an item can have. In my case, there was 2 major versions of the document 1 with 120 draft versions and the other 350+. So you will also need to make sure to set the Draft Item Security and/or the approval setting as well. This will making the users have to publish a major version. It also doesn't stop a owner/designer of the site to go into the settings of the library/list and change them either. But you can also create special permission levels to give you the autonomy to prevent this. The drawback is more admin work on your site.
I know this is a pain point, because I have admin a farm that had issues with versioning that is out of control. (ex. 15GB library with one document taking up 7GB of space. The file was only 15MB in size.)
No code solution
It will be assumed the reader has a good amount of experience with changing library settings, working with site settings, create templates(list and sites), and security within a site collection for no code solution.Now there are a few ways to accomplishing setting the default values without coding, but there will be limitations. For instance, SharePoint has no control of how many draft versions an item can have. In my case, there was 2 major versions of the document 1 with 120 draft versions and the other 350+. So you will also need to make sure to set the Draft Item Security and/or the approval setting as well. This will making the users have to publish a major version. It also doesn't stop a owner/designer of the site to go into the settings of the library/list and change them either. But you can also create special permission levels to give you the autonomy to prevent this. The drawback is more admin work on your site.
How to do it
- Create a new library/list based off of the template you want.
- Change the version settings set to the defaults(library/list settings > Versioning Settings)
- Save this new library as a template in the library/list settings page.
- Now simply create new lists/libraries with this template.
- SharePoint Server (not foundation) you can go into the site settings and limit the templates that are allowable. Go to Site settings for this.
Drawbacks to this approach
- You will have to replicate this template across all site collections.
- This doesn't affect existing lists/library. Manually (or with Powershell) make the changes is required.
- Doesn't stop owners/designer with permissions to make changes to this.
So what are the alternatives?
Powershell Approach
Powershell is a great tool for admins to get this enforce. A simple script can be created to go through all libraries and lists within the farm and make the necessary changes. Before doing this, I would advise first creating a script to inventory this information with what sites/webs are using it and want libraries/lists. There might be some libraries in the farm that shouldn't have the limits you are about to set applied (for legal or regulatory reasons). Best to verify with the teams that own the sites of what you are about to do. This way you can create a file of "exception to the rules" to bypass them when executing the final script. Also, you can set up this script to execute regularly with Windows Task Scheduler. Definitely test the script out first and verify everything is good, especially performance. You don't want to have your environment come to crawl because this script is running at peak times.
How to do it
There are a few articles/posting about scripts to accomplish this
Drawbacks
- Possible performance hit
Code approach
Talk with your SP Architect/SP Developers, they are the main people to consult about this. If they can't figure this out, then you might need to find one who can. But there a lots of options you can do for this:
- List/library definitions - This is a simple coding exercise of creating the list/library definition with the elements.xml. Drawback is that it won't affect pre-existing libraries/lists. It also can be changed by owners/designers in the site collection.
- Event receivers - Use the code to set the defaults and monitor any changes that are against the policies. This is not too bad of solution as(combining with Powershell) it will affect existing and new lists/libraries. Another implementation would be when an item gets added you can check how many draft versions are currently on the item and code something to either remove an older draft version or notify the user they should publish a major version soon.
If you are looking on how to code it use the c-sharpconer.com article above as that article has both the way to do it in C# and Powershell.
Other approaches...
Find a 3rd party tool to do the job. I think Axceler's Control Point might be able to accomplish this.
There is also a CodePlex project as well SharePoint Versioning Manager.
I do not endorse/advise to use either of these options. You should do the leg work and analysis with any tool to verify it meets your needs and doesn't cause issues with your environment. Just present both as options.
Friday, May 17, 2013
Here we go...
So I was attending the SharePoint Best Conference 2013 this week and had a person ask me what was my twitter/blog/facebook? I'm afraid I told that person I have none of that. Often thought the whole blog/twitter was a silly concept and very weird to be posting about me and things I do. But then I started thinking about my career and what my current colleagues at work are trying to promote. Collaboration and social compents in our SharePoint environment. So I figured I would give it a whirl. Never been good at grammar or writing, so bare with me in any of the posts I push out.
I also felt a bit inspire by a co-worker who uses his blog to help with what he has done, like code examples or confugrations to fix an issue. I am kinda leaning to that idea as well. Though I haven't done too much development of late, I am doing some. So I will share here.
First off, the SharePoint Best Conference 2013 was a nice small little gig. Had 2 seminars/lecturers going on at the same time and the presentors were working professionals discussing their trivials and tribulations with their SharePoint experience and what it has to offer to a corporation/business. There was roughly around 150 attendees so allowed people to talk amongst themselves and get discussions going on the topic that is dear to my heart, SharePoint. The main goal was to talk to more towards the Business side than the tech side. Which was a good target audience. I think it help give some ideas and understandgin to the non-technical. There was some geek-speak that I could relate to and some pointers I picked up on how to talk to the business as well.
I attended the 3 days and was worth it. Not sure if I will be back as I love the tech more than the business side. Some of the key points I got out of the conference is
Till next time...
I also felt a bit inspire by a co-worker who uses his blog to help with what he has done, like code examples or confugrations to fix an issue. I am kinda leaning to that idea as well. Though I haven't done too much development of late, I am doing some. So I will share here.
First off, the SharePoint Best Conference 2013 was a nice small little gig. Had 2 seminars/lecturers going on at the same time and the presentors were working professionals discussing their trivials and tribulations with their SharePoint experience and what it has to offer to a corporation/business. There was roughly around 150 attendees so allowed people to talk amongst themselves and get discussions going on the topic that is dear to my heart, SharePoint. The main goal was to talk to more towards the Business side than the tech side. Which was a good target audience. I think it help give some ideas and understandgin to the non-technical. There was some geek-speak that I could relate to and some pointers I picked up on how to talk to the business as well.
I attended the 3 days and was worth it. Not sure if I will be back as I love the tech more than the business side. Some of the key points I got out of the conference is
- Search was vitally important to a good implmentation of SharePoint.
- Know who your audience is and how to talk to them.
- SharePoint is a complicated platform with tons of features that no one can be a complete expert of the whole suite.
- Get the users invovle early and often. Kepp them invovle throughout.
- Governance is good, but don't make it too restrictive.
Till next time...
Subscribe to:
Comments (Atom)