Pages

Wednesday, 21 September 2011

Create a new term in Existing Group and Term Set Under Managed Metadata Service Application with PowerShell


Use this script if you want to create a new term in the SharePoint 2010 Managed Metadata Service with PowerShell:

#Connect to Central Admin
$taxonomySite = get-SPSite http://centraladminsite:port

#Connect to Term Store in the Managed Metadata Service Application
$taxonomySession = Get-SPTaxonomySession -site $taxonomySite
$termStore = $taxonomySession.TermStores["Managed Metadata Service"]
write-host "Connection made with term store -"$termStore.Name

#Connect to the Group and Term Set
$termStoreGroup = $termStore.Groups["Group Name"]
$termSet = $termStoreGroup.TermSets["Term Set Name"]

#Create term, term description, and a synonym
$term = $termSet.CreateTerm("Test Term", 1033)
$term.SetDescription("This is a test", 1033)
$term.CreateLabel("This is a test synonym", 1033, $false)

#Update the Term Store
$termStore.CommitAll()

Thursday, 8 September 2011

Yield keyword in C Sharp

One of the cool and yet most unknown feature of the C# is the yield keyword. The yield keyword is used in an iterator block to provide a value to the enumerator object or to signal the end of the iteration. When used the expression is evaluated and returned as a value to the enumerator object. Note the expression has to be implicitebly convertible to yield type of the iterator. Here is an example

public static IEnumerable<int> GetIntCollectionFromString(string SomeString)
{
string[] val = SomeString.Split(' ');
int intVal;
foreach (string token in val)
{
if (int.TryParse(token, out intVal))
{
yield return intVal;
}
else
{
yield break;
}
}
}

Here since we are using the yield keyword the function will return the collection of intVal instead of the value of intVal. The statement will only return when the iteration of the loop is complete.

There are some limitation with the yield keywords.
Unsafe blocks are not allowed
Parameters to the method, operator, or accessor cannot be ref or out.

Wednesday, 7 September 2011

How To Create an ASP.NET HTTP Handler by Using Visual C# .NET

Implement the Handler
Open Microsoft Visual Studio .NET. In Visual C# .NET, create a new Class Library project named MyHandler.
Set a reference to the System.Web.dll assembly.
Add the following directive to the class:
using System.Web;

Rename the class SyncHandler.cs, and then change the class definition to reflect this.
Implement the IHttpHandler interface. Your class definition should appear as follows:
public class SyncHandler : IHttpHandler

Implement the IsReusable property and the ProcessRequest method of the IHttpHandler interface. Because this is a synchronous handler, return False for the IsReusable property so that the handler is not pooled.
public bool IsReusable
{
get {return false;}
}

public void ProcessRequest(HttpContext context)
{
context.Response.Write("Hello from custom handler.");
}
Compile the project.


Deploy the Handler
Create a new directory named Handler under the C:\Inetpub\Wwwroot directory.
Create a subdirectory named Bin in the newly created Handler directory. The resultant path is C:\Inetpub\Wwwroot\Handler\Bin.
Copy MyHandler.dll from your project's Bin\Debug directory to the C:\Inetpub\Wwwroot\Handler\Bin directory.
Follow these steps to mark the new Handler directory as a Web application:
Open Internet Services Manager.
Right-click the Handler directory, and then click Properties.
On the Directory tab, click Create.
Follow these steps to create an application mapping for the handler. For this handler, create a mapping to the Aspnet_isapi.dll file for the *.sync extension. Whenever a .sync file is requested, the request is routed to ASP.NET, and ASP.NET executes the code in the handler.
Right-click on the Handler Web application, and then click Properties.
On the Directory tab, click Configuration.
Click Add to add a new mapping.
In the Executable text box, type the following path: Microsoft Windows 2000:
C:\WINNT\Microsoft.NET\Framework\<version#>\Aspnet_isapi.dll
Microsoft Windows XP:
C:\WINDOWS\Microsoft.NET\Framework\<version#>\Aspnet_isapi.dll
In the Extension text box, type .sync.
Make sure that the Check that file exists check box is cleared, and then click OK to close the Add/Edit Application Extension Mapping dialog box.
Click OK to close the Application Configuration and the Handler Properties dialog boxes.
Close Internet Services Manager.


Configure the System
In the C:\Inetpub\Wwwroot\Handler directory, create a new file named Web.config.
Add the following code to Web.config:
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="*.sync" type="MyHandler.SyncHandler, MyHandler" />
</httpHandlers>
</system.web>
</configuration>

In the verb="*" attribute, we instruct the handler to process a request that uses any verb (for example, POST, HEAD, GET, and so on). If you want this handler to process only the POST request, change this to verb="POST".

In the path="*.sync" attribute, we instruct the handler to process any incoming requests for files with the .sync extension.

In the type="MyHandler.SyncHandler, MyHandler" attribute, we instruct the handler that processes the request to implement in the MyHandler.SyncHandler namespace, and this class resides in the MyHandler assembly.


Test the Module
To test a handler, a page does not need to exist in the file system. For example, request the Default.sync file in the Handler Web application (http:///Handler/Default.sync). You should receive the following results:
Hello from custom handler.




Popular Posts