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);
   }
}
Advertisements

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>

Resetting the DNS server list to static in Azure

The SharePoint 2013 development environment in Azure that i maintain contains a fully fledged AD/DNS infrastructure. To keep things lean, i did not want to maintain a separate infrastructure VM and just combine the roles. However, every time the machine is restarted, the IP interface settings needs to be adjusted. I need to ensure that the Azure provided DNS server needs to be come secondary DNS server in the list while loopback address stays as primary. I have not fully automated this just yet, but here is the command-line methods to get it done.

netsh interface ip "Ethernet 8" add dnsserver 127.0.0.1 primary
netsh interface ip add dnsserver "Ethernet 8" 100.112.12.40 index=2

To update the DNS services forwarder to the new server ip address:

import-module DnsServer
Set-DnsServerForwarder -IPAddress 100.112.12.40

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.