Monday, May 10, 2010

SharePoint HttpHandler Example

This past week I needed a solution to do customized server-side work from the EditControlBlock menu in a list. I had to lace together a few solutions that I found when googling the net.


1) Creating a CustomAction as a feature to install on the SharePoint server.
2) Creating a custom WebControl for the CustomAction.
3) Utilize jQuery and a Generic Handler to perform server side code.

For the CustomAction it was pretty easy. You need to place the following XML in the elements.xml file for your feature.


elements.xml
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="Unique Id Here"
Title="Action Name in Menu"
Description="Description of Action"
RegistrationType="List"
RegistrationId="101"
Location="EditControlBlock"
Sequence="1000"
ControlAssembly="[Fully Qualified Assembly Name]"
ControlClass="ImportXML.Import">
<UrlAction Url="javascript:ExecuteAJAXCall();"/>
</CustomAction>
</Elements>

This will create a CustomAction for the Edit Control Block menu. The Edit Control Block menu is the pop down menu that appears for List Items. RegistrationId can change based on the type of list you want to attach this CustomAction to.

The second part is creating a generic handler to handle the server side functionality.
HandlerRequest.ashx
<%@ Assembly Name="[Fully Qualified Assembly Name]" %>
<%@ WebHandler Language="C#" CodeBehind="HandlerRequest.ashx.cs" Class="HttpHandler.HandlerRequest" %>
HandlerRequest.ashx.cs
namespace HttpHandler
{
public class HandlerRequest : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
string webUrl = context.Request.Form["thisWebUrl"];
string listName = context.Request.Form["thisListName"];
string stringListId = context.Request.Form["thisListItemId"];

int itemId = Convert.ToInt32(stringListId);

List<string> response = new List<string>();

try
{
using (SPSite site = new SPSite(webUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists[listName];

SPListItem item = list.Items[itemId];


response.Add("1");
response.Add(item.ID.ToString());
response.Add(item.Title);
}
}
}
catch(Exception ex)
{
response.Add("0");
response.Add("Operations failed.");
response.Add(ex.Message);
response.Add(ex.StackTrace);
}

JavaScriptSerializer JSSerializer = new JavaScriptSerializer();
context.Response.ContentType = "text/plain";
context.Response.Write(JSSerializer.Serialize(response));
}

public bool IsReusable
{
get
{
return false;
}
}
}
}


Finally I am using javascript in the default.master to compose the ExecuteAJAXCall() method. I utilized SharePoint Designer to access default.master.
Javascript
<script type='text/javascript' src='jquery.js' ></script>
<script>
function ExecuteAJAXCall() {
var userResponse = confirm("Are you sure you want to import this data?");

if (userResponse) {
$.post("/_layouts/ImportXML/ImportRequest.ashx",
{thisWebUrl:ctx.HttpRoot,thisListName:ctx.ListTitle,thisListItemId:currentItemID},
function(data, status, request){
Callback_AJAXCall(data, status, request);
},
'json'
);
}
};

function Callback_AJAXCall(data, status, request) {
if (data[0] == "1") {
alert(data[1]);
} else {
alert(data[1] + "\r\n\r\nError Message:" + data[2] + "\r\n\r\nStack Trace:" + data[3]);
}
};
</script>


After the creation of all these files you are ready to start putting it all together for the server.

1) The generic handler needs to be placed in the "12\TEMPLATE\LAYOUTS" folder.
2) The .dll file from the handler code needs to be placed in "InetPub\wwwroot\wss\VirtualDirectories\80\bin"
3) Last step is to install the feature through stsadm.exe command line utility and you are set!

Hope that helps out someone.

Thursday, January 21, 2010

Silverlight TextBlock Control

The TextBlock control in Silverlight is used in the same manner as an ASP Label control. However, from my experience you have a lot more control with a TextBlock control.

There is some background you should know about Silverlight controls. Silverlight uses a mark up language known as XAML. The Silverlight Controls can have attributes embedded in a tag or they can be in the sub elements of a control. For example when you want to change the Text of a TextBlock control you have multiple options to do this. Here are a couple of examples.

or...


Both of these code blocks will do the exact same thing. Explore through the attributes to see what you can get out of them. You can create a bit more robust text with some effort.

You can put your own creative spin on Text in a text block without having to go in to the code behind to do something really crazy like this...


So you can see that there is some great power behind the embedded tags on the XAML side. Check out all the different options that you have available to build some very robust Labels.

Wednesday, January 20, 2010

Interested Topics

Languages
  • C#
  • Java
  • Groovy
  • Grails
  • XBasic
  • XAML
  • T-SQL (<-- does this count?)
Technologies
  • ASP.NET
  • Silverlight
  • SharePoint
  • AlphaFive
  • WPF

IDEs
  • Visual Studio 2005/2008/2010
  • AlphaFive Software
  • Eclipse
  • NetBeans

Experience
  • Full Life Cycle development
  • Prototyping
  • Documentation
  • Web Development


I have a degree in Computer Science, and I didn't start a career in software engineering until after my time in the military. I have only been at this for less than a year so far.

Is there something about the Software Engineering community that I missing? I get the feeling that people stick to one know how and let that be their weapon of choice. Is this true? My perspective about life is to learn as much as possible. Is that a waste of time. I am taking the necessary steps to learn new languages, technologies, etc. to be more versatile. However, the people that work around me are focused on one area (i.e., Desktop Applications with C# and SQL or Web Development with Groovy & Grails). Am I just missing something here?

I am not talking about just scraping the top of these techniques and procedures. I dive into the deep end. Whether that is reading a book from cover to cover, or searching the internet for blogs, forums, nick nacks, etc. to get the ins and outs. I have recreated a web application in two different ways to "prototype" it.

I would like to know if I am headed down the wrong path with being so versatile. Someone let me know if I need to be focusing in on something.