Point Deep

Mundeep's Tech Blog

Active Directory Users and Computers MMC Snapin for Windows 2008

Posted by mundeep on December 28, 2011

Having recently been required to do a fresh install of Windows 2008 R2 for a new development server i was having trouble finding where to install the Active Directory Users and Computers MMC snapin.

Essentially you need to install the “Active Directory Domain Controller Tools” via:

Server Manager -> Features -> Add Features

And then: Remote Server Administration Tools -> Role Administration Tools -> Active Directory Domain Services Tools -> Active Directory Domain Controller Tools

Of all the places on the web i actually found this answer via the Microsoft Connect forums
http://connect.microsoft.com/WindowsServerFeedback/feedback/details/490592/

Posted in Uncategorized | Tagged: | Leave a Comment »

Sharepoint list view threshold and working with large lists reference

Posted by mundeep on November 3, 2011

A common error to come across in Sharepoint 2010 when dealing with large lists is:

The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.

The following blog articles have a good explanation on how the thresholds work in Sharepoint 2010.

Microsoft also has some official guidance on managing large lists:

Posted in Uncategorized | Leave a Comment »

Attaching to the right process when debugging

Posted by mundeep on August 18, 2011

A common problem when trying to debug an ASP.NET web application (including SharePoint based solutions) is determining which w3wp.exe process to attach to. Thankfully there are a couple of commands to help out.

If using IIS7:
c:\Windows\System32\inetsrv\appcmd list wp

If using IIS6:
cscript c:\WINDOWS\system32\iisapp.vbs

Source: http://stackoverflow.com/questions/748927/iis-application-pool-pid

Posted in .NET | Tagged: , , , , | 2 Comments »

Sharepoint 2010 Remote Debugging

Posted by mundeep on August 5, 2011

The following is a realy good article about Remote Debugging Sharepoint 2010 webparts and worth a read if you are ever having trouble getting it working:
http://lemonharpy.wordpress.com/2011/03/17/remote-debugging-sharepoint-2010-solutions/

Essentially the main things to ensure are:

  1. You are running Visual Sudio Remote Debugger (msvsmon.exe)
  2. You are using an account with permissions to debug.
  3. You have placed the pdb files in the approriate folder (usually in GAC)
  4. The version of code running on the remote server is exactly the same as in the Visual Studio project you have opened (If possible it is always good to perform fresh build and deployment before trying to debug.

Posted in .NET, Sharepoint | Tagged: , , | 1 Comment »

c# if else alternative syntax using ? : operator

Posted by mundeep on July 15, 2011

One of the c# operators i tend to use a lot is the ?: operator which is essentially an alternative syntax to an if else statement. Unfortunately i often forget the order of the statements so am adding this quick post as a note for myself to easily remember :)

Essentially:

condition ? first_expression : second_expression;

Is equivalent to:

if (condition) {
 first_expression;
}
else {
 second_expression;
}

References:

Posted in .NET | Tagged: , , | 1 Comment »

Powershell Script to Import Sharepoint Organization Profiles from Active Directory

Posted by mundeep on November 8, 2010

Here is one of my first Powershell scripts (so improvement suggestions welcome :)

The requirement was to script the creation of SharePoint Organization Profiles from Active Directory Organizational Units, including building the hierarchy and setting the Profile Subtype appropriately. Creation of the Organizational Profiles was based on the C# code in the How to: Create User Profiles and Organization Profiles MSDN article.

Before running this note that i had a predefined list of profile subtypes that corresponded to the level of the organizational unit in the hierarchy. The top level was Root, next level was Group, then Division, Branch & finally Section. The following is an example of the organizational structure in terms of Profile Subtypes.

  • Department (Root Level)
    • Group #1
      • Division #1
        • Branch #1
          • Section #1
      • Division #2
        • Branch #2
          • Section #2
          • Section #3
          • Section #4
        • Branch #3
          • Section #5
          • Section #6
      • Division #4
        • Branch #7
    • Group #2
      • Division #5
        • Branch #8
          • Section #9
      • Division #6
        • Branch #9
          • Section #10
          • Section #11
          • Section #12
      • Division #6
        • Branch #9

#Create All Organisation Profiles from AD OU's
#Load Sharepoint Snapin
Add-PSSnapin Microsoft.SharePoint.PowerShell
#Declare Some Globals for Profile Management
$context = Get-SPServiceContext -Site http://{sharepointwebappurl}/
$psm = [Microsoft.Office.Server.UserProfiles.ProfileSubtypeManager]::Get($context);
$opm = New-Object Microsoft.Office.Server.UserProfiles.OrganizationProfileManager($context)
$rootOrg = $opm.RootOrganization

#Find All Children OU's Based on Parent OU's Distinguished Name (DN)
function Find-Child-OUs
{
Param ([string]$sParentDN)
	$ldapPath = "LDAP://" + $sParentDN
	$query = new-object system.directoryservices.directorysearcher([ADSI]$ldapPath)
	#Only Search for OUs
	$query.filter = "(objectClass=organizationalUnit)"
	#Only Search One Level Deep (ie. Children)
	$query.SearchScope = "OneLevel"
	$results = $query.findAll()
	Write-Host $sParentDN " has " $results.Count " children"
	return $results
}

#Create an Organization Profile under parentOrg for provided OU
function Create-Organization {
Param ($ou, $parentOrg, $levelTitle)
	Write-Host "Creating:" $ou.description "under" $parentOrg.DisplayName "of type" $levelTitle
	$subType = $psm.GetProfileSubtype($levelTitle);
	$orgProfile = $opm.CreateOrganizationProfile($subType, $parentOrg)
	$orgProfile.DisplayName = $ou.description
	$orgProfile.Commit()
	return $orgProfile
}

#Determine Subtype Based on Level
function Get-Level-ProfileSubtype {
Param ($level)
	$subtypeTitle = $level
	switch ($level) {
		0 { $subtypeTitle = "Root"; break }
		1 { $subtypeTitle = "Group"; break }
		2 { $subtypeTitle = "Division"; break }
		3 { $subtypeTitle = "Branch"; break }
		4 { $subtypeTitle = "Section"; break }
		default { $subtypeTitle = "Other" }
	}
	return $subtypeTitle
}

#Create Organization Profiles for All OU's underneath the provided OU and Organization Profile
function Create-Child-Orgnizations
{
Param ([string]$currentOuDn, $parentOrg, $level)
	#Create the Organization
	Write-Host "Searching for Children for: " $currentOuDn
	$children = Find-Child-OUs $currentOuDn
	if ($children -ne $null) {
		Write-Host -ForegroundColor red $children.Count "OU's found under:" $currentOuDn
		Foreach($childOu in $children)
		{
			$ou = $childOu.GetDirectoryEntry()
			$ouDn = $ou.distinguishedname
			$levelTitle = Get-Level-ProfileSubtype($level)

			#Create the Child Organization
			$newOrg = Create-Organization $ou $parentOrg $levelTitle
			Write-Host -ForegroundColor green "OU Created: " $newOrg.DisplayName " = " $newOrg.ProfileSubtype.Name
			$nextLevel = $level + 1
			Create-Child-Orgnizations $ouDn $newOrg $nextLevel
		}
	}
}

#Create All Organization Profiles Starting from Provided OU DN
function Create-All-Organizations {
Param ([string]$rootOuDn)
	Create-Child-Orgnizations $rootOuDn $rootOrg 1
}

Once you have loaded all the globals and declared all the functions above the following two lines (can be done in one) will begin the profile creation.

#Run Creation From Root Orgnization Unit, Make sure the following matches the LDAP query to the Root OU in your AD structure.
$rootOu = "OU=Department,DC=depttest,DC=gov,DC=au"
Create-All-Organizations $rootOu

To help with testing & re-running code i also created a couple of functions to (a) List all Organizational Profiles under a parent and (b) Delete all Organization Profiles under a parent.

#Retrieve existing Org Profiles
function List-OrgProfiles {
Param($parentOrg)
	if ($parentOrg.HasChildren) {
		foreach ($childOrg in $parentOrg.GetChildren()) {
			Write-Host $parentOrg.DisplayName "-" $childOrg.DisplayName
			List-OrgProfiles $childOrg
		}
	}
}

#Delete existing Org Profiles
function Delete-OrgProfiles {
Param($parentOrg)
	# If Parent Has Children Delete them first
	if ($parentOrg.HasChildren) {
		foreach ($childOrg in $parentOrg.GetChildren()) {
			#Write-Host $childOrg.DisplayName " - " $parentOrg.DisplayName
			Delete-OrgProfiles $childOrg
		}
		#Once children are deleted remove the current org
		if ($parentOrg.RecordId -ne $opm.RootOrganization.RecordId) {
			$opm.RemoveOrganizationProfile($parentOrg.RecordId)
			Write-Host "Removing(" $parentOrg.DisplayName ")"
		}
		else {
			Write-Host "KEEP (" $parentOrg.DisplayName ")"
		}
	}
	else {
		#If not children then delete the current org
		if ($parentOrg.RecordId -ne $opm.RootOrganization.RecordId) {
			$opm.RemoveOrganizationProfile($parentOrg.RecordId)
			Write-Host "RemovingChildless(" $parentOrg.DisplayName ")"
		}
		else {
			Write-Host "Childless KEEP(" $parentOrg.DisplayName ")"
		}
	}
}

Posted in Powershell, Sharepoint | Tagged: , , , , | Leave a Comment »

Bug in Editing Sharepoint Organization Profiles

Posted by mundeep on October 27, 2010

There appears to be a bug in editing Sharepoint 2010 Organization Profiles. If you try to edit the Profile SubType without editing any other field your change is not saved. The work around is to update any other field at the same time (and then don’t forget to change that field back).

Refer to the comments on MSDN http://msdn.microsoft.com/en-us/library/ms545122.aspx for an example on how to do this in code.

Posted in Sharepoint | Leave a Comment »

Sharepoint 2007 Calculated Fields

Posted by mundeep on October 27, 2010

This is just a quick brain dump of some research into Calculated Field formulas for MOSS that i’ve had sitting as a Draft for a while.

UPDATE: This appears to be the most comprehensive offical guide on Formulas and Functions that can be used:
http://office.microsoft.com/en-us/windows-sharepoint-services-help/CH010065006.aspx

Posted in Sharepoint | Leave a Comment »

Updating the Created Date of a Document or List Item Programmatically

Posted by mundeep on May 22, 2009

A common tasks developers are required to do with Sharepoint is migrated documents from either legacy systems or older Sharepoint sites into a document library. There are many ways to do this documented out there (most commonly using the Files.Add method of the API), but one of the common requirements during this ‘migration’ is to retain the Created Date or Created By fields. Sowmyan’s blog has a good description of how to set the Created By/Modified By user fields here.

Updating the Created Date of a document or list item in Sharepoint is even easier, simply set the “Created” field of the item to the value you wish to set. For example if using the Files.Add methods in the API (as described by Dave Hunter) then the following code snippet will update the Created Date:

// add the file   
SPFile file = docLib.RootFolder.Files.Add(newFileName, inputStream);   
// get the list item for that file   
SPItem item = file.Item;   
//Set the Created Date
item["Created"] = "2009-02-26 15:00:00";
item.Update();

Posted in .NET, Sharepoint | Tagged: , , , | Leave a Comment »

InfoPath 2007 Custom Field Validation (Programmatic)

Posted by mundeep on May 20, 2009

Adding custom (programmatic) field validation to a form in InfoPath 2007 is quite easy. Firstly make sure you have set your programming language of choice by going to the Tools menu then “Form Options” -> “Programming”, and then selecting your preferred language under “Form template code language”. For this example i am going to be using C#.

Back in your form design window right-click on the field you wish to validate, and select “Programming” -> “Validating Event”. This will open a “Visual Studio Tools for Applications” (VSTA) window, and generate the following code snippet:

        public void myFieldName_Validating(object sender, XmlValidatingEventArgs e)
        {
            // Write your code here.
        }

Obviously this code is not doing any validation yet, we just need to insert our validation code in place of the “your code here” comment. Let us try a simple validation of ensure the data entered in the field is at least 3 characters long with the following code.

public void myFieldName_Validating(object sender, XmlValidatingEventArgs e) {
    //Check the Length of the NewValue of the field
    if (e.NewValue.Length < 3) {
        //Report the validation error back to the client on the form
        e.ReportError(e.Site, false, "Please Enter a valid MyField");
    }
}

As mentioned in the comments e.NewValue retrieves the ‘New’ value that is being entered/inserted into the field before it has been set and e.ReportError sends any validation errors back to the form for the client (read the MSDN article for details on the parameters of ReportError).

Unfortunately there is a minor issue with this basic piece of code and that is due to the fact that the validating event also gets fired during the loading of the form and as shown in this kb article we need to check the XmlOperation that is being performed when the validation is fired. This means updating our code to the following.

public void myFieldName_Validating(object sender, XmlValidatingEventArgs e)  {
    //Ensure validation is only done when inserting or changing a value
     if (e.Operation == XmlOperation.Insert || e.Operation == XmlOperation.ValueChange) {
        //Check the Length of the NewValue of the field
        if (e.NewValue.Length < 3) {
            //Report the validation error back to the client on the form
            e.ReportError(e.Site, false, "Please Enter a valid MyField");
        }
    }
}

Save your code changes and while still in VSTA build the project to ensure you don’t have any coding errors (Ctrl+Shift+B, or from the menu “Build” -> “Build ‘MyFormName’”). If you get a “Build succeeded” message in the status bar you can close the VSTA window and then preview your form to see your validation in action.

Posted in .NET, Office | Tagged: , , , , | 1 Comment »

 
Follow

Get every new post delivered to your Inbox.