Thursday, June 6, 2013

Deleting a WebPart from the WebParts Gallery using PowerShell

So you have a requirement to remove a webpart or multiple webparts from the webparts gallery in SharePoint and you need to do this with Windows PowerShell. One thing to remember is that the webparts gallery is the same as any other gallery in SharePoint and your task can easily be achieved using PowerShell. The below code will remove a webpart from the webparts gallery:

function RemoveWebPartsFromGallery([string]$siteUrl)
{
# an array of the custom webparts that were built;
$customWebPart = New-Object System.Collections.ObjectModel.Collection[System.String]
$customWebPart.Add("CustomWebPart")

$site = Get-SPSite $siteUrl

# the default SP type for a webpart gallery;
$wpCatalog = [Microsoft.SharePoint.SPListTemplateType]::WebPartCatalog

$list = $site.GetCatalog($wpCatalog)

$wpID = New-Object System.Collections.ObjectModel.Collection[System.Int32]

foreach ($item in $list.items)
{
foreach ($wps in $customWebPart)
{
if ($item.Name.ToLower().Equals($wps))
{
$wpID.Add($item.ID)
continue
}
}
}

foreach ($i in $wpID)
{
$wpitem = $list.GetItemById($i)
$wpItem.Delete()
}

$list.Update()

Write-Host "WEBPARTS REMOVED FROM GALLERY" -ForegroundColor Green
}

You can check out the original script here a big thanks to Srinivas Challagolla for the script. 

The script has greatly helped me I hope that it has helped you. Happy scripting guys :-)

Wednesday, May 29, 2013

STSADM VS POWERSHELL SOLUTION DEPLOYMENT

PowerShell Deployments

So everyone is familar with STSADM command prompt and we have all learned to love it. So now we have PowerShell, so to get you started please have a look at the following links which are great resources to get you started for deploying a "WSP" Package using powershell.
There are many more resources available out there - but those are the two I decided to use. You can see the basic commands for deployment below:


Add Solution

ADD-SPSOLUTION [SOLUTION LOCATION]

Deploy Solution

INSTALL-SPSOLUTION -IDENTITY [SOLUTION NAME] -GACDEPLOYMENT -WEBAPPLICATION [URL]

You can also check out a few MSDN articles:


Hope these articles help you out :-)

SharePoint Timer Jobs

How to create a SharePoint 2010 Timer Job...

So I have finally found sometime to create a post on how to create a SharePoint 2010 timer job. I had a look around and found many, many post already out there and they are awesome.

Please have a look at Andrew Connels blog post as it is a great resource to use for creating SharePoint timer jobs.

Another resource that I found very useful was SharePoint Tips for creating various timer job schedules.

So happy coding everyone, I hope these resources can help you.

Tuesday, May 14, 2013

Deploy SharePoint 2010 master page using a module

So this post may or may not be a bit overdue, but deploying various files with a module makes life for us as developers pretty simple. In this post I will detail the steps to deploy your custom developed SharePoint 2010 master page into the master page gallery.

So the first step is to create a new module in Visual Studio





















By default the following files below are you created for you when a module is created, you can go ahead and delete the "Sample.txt" file as we will add our master page shortly.










You can down download a fantastic starter master create by Randy Drisgill from the following link.

Once you have downloaded the master page, add the master into the module.

Now once the master page has been added - open the "Elements.xml" file and add replace with the following:












And it is as simple as that - you can now deploy your custom built master page into the master page gallery with ease. I hope that this post has helped you. 

Happy Branding :-)

SharePoint 2010 Test Cases including Test Matrix

In the last few months we were tasked to create test cases including a test matrix for what was developed in our SharePoint 2010 solution. So I started out on my Google Search trying to find a generic test document which I can reuse across my site as I did not want to write any test cases for the OTB functionality that SharePoint offers.

I found this link to provide good guidance in formulating a test plan as well as a test matrix document.

There are various other sources available on the Internet, but I have found the one mentioned above most useful for my specific requirements.

I hope this helps :-)

Thursday, May 9, 2013

How to create a site collection and specify the content database

So pretty much everyone knows how to create a new site collection, but what is not so simple is creating a new site collection in a content database which you specified. This can easily be achieved using some powershell, as shown below:


$SITECOLLECTION = Read-Host "What is the name of the site collection you wish to add?"
$SITEDESCRIPTION = Read-Host "What is the site collection description?"
$SITEURL = Read-Host "What is the URL of the site you wish to add?"
$SITEADMINISTRATOR = Read-Host "Who is the site collection administrator?"
$DATABASENAME = Read-Host "In which content database do you wish to add this site collection?"
$SITETEMPLATE = "CMSPUBLISHING#0" # --> THIS IS SITE TEMPLATE FOR PUBLISHING SITE

# CHECK IF CONTENT DATABASE EXIST

$CONTENTDB = Get-SPContentDatabase -identity $DATABASENAME

# IF CONTENT DB EXISTS WE WILL ADD SITE COLLECTION AND USE CONTENT DB SPECIFIED ELSE DISPLAY ERROR MSG

If (!$CONTENTDB)
{
Write-Host "The specified content database has not been found - PLEASE TRY AGAIN"
}
else
{
Write-Host "SITE COLLECTION CREATION STARTED"

New-SPSite -Name $SITECOLLECTION -Url $SITEURL -Template $SITETEMPLATE -OwnerAlias $SITEADMINISTRATOR -ContentDatabase $CONTENTDB -Description $SITEDESCRIPTION

Write-Host "SITE COLLECTION CREATION COMPLETE"

}

I hope this helps you as it did me.

Wednesday, May 8, 2013

How to retrieve s user profile in SharePoint 2010

The below code will show how to retrieve a user profile using the SharePoint object model

The following references are required:
  • Microsoft.Office.Server
  • Microsoft.Office.Server.UseProfiles
  • Microsoft.SharePoint
  • System.Web  
        private UserProfiles.UserProfile GetCurrentUserProfile(SPUser currentUser, string siteCollectionUrl)
        {
            try
            {
                using (var site = new SPSite(siteCollectionUrl))
                {
                    var serverContext = SPServiceContext.GetContext(site);
                    var profileManager = new UserProfileManager(serverContext);
                    var userProfile = profileManager.GetUserProfile(currentUser.RawSid);

                    return userProfile != null ? userProfile : null;
                }
            }
            catch (Exception ex)
            {
                // Handle error and write them to a log
                return null;
            }
        }


I hope this has helped you, please feel free to drop me a note if you have any questions.