Check for Group existence in SharePoint Site

In SharePoint 2013 server side object model (SSOM), it appears that the Method overloads of the SPGroupCollection such as GetByName() or indexer all throw an exception when the group is not found.  However, you can still use reflection to get non throwing version of the methods. In my project I ended up creating an extension method that wraps the reflection based function call.

public static class SPGroupCollectionExtensions
{
  public static SPGroup GetByNameNoThrow(this SPGroupCollection group, string groupName)
  {
    var method = group.GetType().GetMethod("GetByNameNoThrow", BindingFlags.Instance | BindingFlags.NonPublic);
var parameters = new object[] {groupName};
    return (SPGroup)method.Invoke(group, parameters);
   }
}
Advertisement

User Profile Synchronization Service won’t start due to PowerShell profile

Recently, I had been asked by the colleague to help them troubleshoot the issue where User Profile Synchronization service would hang during the attempt start.

There were no error logs except that it would return to Stopped state. We found out that if the account that you login is the same account that you run the User Profile Synchornization Service and that account has a powershell profile, then it causes the issue. It appears that during the start process of the UPS Synch service, it runs the PowerShell runspace with the default shell as opposed to creating its custom shell, so default profiles in your documents folder would be loaded. Thus, the issue.

Best way to resolve is to ensure that even in your dev environment, you must make sure that you’re not running your interactive shell with the account that’s running SharePoint services processes.

View SharePoint Deployment Progress

In the past, I have blogged that when a developer deploys the a WSP package through PowerShell, it’s nice to see if the deployment job succeeds before moving on to the next step.

In the past, this is the command I have used:

Get-SPSolution -Identity SomeSolution.wsp | Select Last* | fl

And waited until all servers in the farm are reporting that deployment is successful.  However, at times the job is still running.  A more robust approach I use now is to look for JobExists property and ensure that it’s false.  Therefore updated command I use now:

Get-SPSolution -Identity SomeSolution.wsp | Select Last*, JobExists | fl

Deleting TFS Work Item

I was creating a number of user stories and associating the Tasks to users, and quickly realized that the TFS 2013 still does not have a way to delete a task.  Woooaahhh!!!

There is a way to do it via command line, via witadmin tool:

witadmin destroywi /Collection:<TFS Collection URL> /id:<id of the task>

Managing Azure dev environment from PowerShell

Recently, I’ve been spending some time prototyping the new SharePoint 2013 App Model in my Azure provision SP2013 development environment.  I wanted to make sure that i can shut it down or start it up quickly.  So PowerShell is of course the answer here.

Small assumption is that the Azure Service Name matches the Virtual Machines. It is possible to have multiple virtual machines provisioned under the same service Name

First, ensure that the valid Azure subscription is attached to the Azure command line session:

Get-AzureAccount

If that returns no subscriptions, then use Add-AzureAccount commandlet to add an active subscription.
To start/stop the Azure Virtual Machines I use these commands:

Get-AzureVM -ServiceName <ServiceName> | Start-AzureVM
Get-AzureVM -ServiceName <ServiceName> | Stop-AzureVM -Force:$true

I’ll add more commands as i start doing more complex things with Azure dev boxes as I go along.

Creating custom Web Part properties in Sandbox Solutions

I am currently working on a quick POC for configuring the Drop-down via custom tool pane.  The scope of my POC is to test whether Visual Web Part development can be robustly achieved via Sandbox Solution deployment model.

I started with a web part property to hold the drop-down as follows:

[Personalizable(true)]
public List<DropDownSelection> DropdownListSelections { get; set; }

After attempting to deploy and use the web part, i quickly ran into an error, “Web part property ‘DropdownListSelections’ uses unsupported type <..>, and cannot be run as a sandboxed code web part.”

Allan Dahls’s blog article astutely points out that only a subset of simple types are supported by the SP2010 infrastructure.  Therefore, my workaround is to use a string datatype for the property and rely on json serialization to persist the actual list.


[Personalizabe(true)]
public string DropdownListSelections { get; set; }

public List<DropDownSelection> DeserializeFromProperty()
{
   var serializer = new JavaScriptSerializer();
   var deserializedProperties = (string.IsNullOrEmpty(DropdownListSelections)) ?
                    new List<DropDownSelection>() : serializer.Deserialize<List<DropDownSelection>>(DropdownListSelections);
   return deserializedProperties;
}

public void SerializeToProperty(List<DropDownSelection> _listToSerialize)
{
   var serializer = new JavaScriptSerializer();
   return serializer.Serialize(_listToSerialize);
}

That allowed me to continue using Sandbox Solution deployment model in my POC.

Quickly updating SharePoint 2010 service account

At my current client, I run a sandbox SharePoint 2010 single server environment on top of Windows 7 with my corporate credential as my service account.

I get a wonderful message saying that my account password is about to expire, so obviously, my heart goes thumping. Here we go again.  It’s time to lose more hours on running around and switching around password within SharePoint 2010 that powers all the web apps, service apps, etc…

However, as per my colleague suggestion, it turned out to be pretty easy:

  1. Change your corporate account password using Ctrl+Alt+Delete -> Change Password
  2. Run the following command in PS to quickly update everything else.
$cred=get-credential;Set-SPManagedAccount -Identity $cred.Username -ExistingPassword $cred.Password -UseExistingPassword

Restart the IIS via “iisreset /noforce” and you’re all set.

Design Manager woes with plain HTML

In our recent projects for SharePoint 2013, we have to use the design manager to build a responsive branding design package.  If you are not familiar with this new feature, please take a look at  Overview of Design Manager in SharePoint 2013 to familiarize yourselves.

One of the challenges that we found with SharePoint 2013 Design Manager is that it modifies and rearranges not only <!–MS/ME/SPM –> markup code, but also the HTML itself during design package import process.

One such example was when we included protocol agnostic script source tag for jquery.


<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

The resultant tag in the imported instance was the following.


<script type="text/javascript" src="/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

After attempting various tricks using <!– –> markup and trying to enclose the html in the design manager tags with no particular success.  The workaround was as follows, after obtaining an answer to our post on MSDN forum:


<!--SPM:<asp:Literal runat="server" Text="&amp;#60;script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js' &amp;#62; &amp;#60;/script&amp;#62;" /> -->

The Literal ASP.Net server control is used to emit raw html that we need during response. Note, that the input has to be html encoded to avoid further issues with Design Manager.  This is really ugly but will do for now.

View SharePoint Deployment progress

There are many instances where our team would deploy the SharePoint solutions through management shell and then opens the Central Administration in the browser to see the deployment progress.
It’s much easier to just write a one liner to see the status instead.

   1: get-SPSolution SampleSolution | Select Last* | format-list