5

SharePoint Simple Event Handler Tutorial: Update Document Library Item Field

Recently someone asked how to update a custom field in a document library with a specific value (which can be a calculated value, custom code which result in a value or even a combination of a lookup from within SharePoint or external sources).

In this particular case the person wanted to set a custom field to contain the value of the "Name" field when a document is uploaded into a document library.

I created the following tutorial which contains the steps needed in order to build and deploy a feature to perform the abovementioned functionality:

Prerequisites:

  • Visual Studio 2008 extensions for Windows SharePoint Services 3.0, v1.3
  • WSS v3 / MOSS 2007
  • Visual Studio - C#
  • A document library which contains a custom field named "MyCustomColumn"
1-Create a new Visual Studio Empty SharePoint Project














2-Add a new Item to the empty project. Select the SharePoint Category and click on Event Receiver. Give it an appropriate name and click on Add.














3-Bind the Event Receiver to a Document Library








4-The following classes are added to your project:


















5-Open the ItemEventReceiver class in code view.
Locate the ItemAdded event. You will notice that all events are commented out, so uncomment the ItemAdded event and add the following code segment inside the event:

public override void ItemAdded(SPItemEventProperties properties)
{
properties.ListItem["MyCustomColumn"] = properties.ListItem["Name"].ToString();
properties.ListItem.Update();
}

Remember that for this example to work all document libraries must contain a custom field named "MyCustomColumn"



6-Ensure that your feature will be deployed to the correct web application.
Go to Project --> Properties --> Debug Tab --> Check the "Start browser with URL" setting.

















7-Build your project.

8-If no errors exist, open the output window and Deploy your project. You will notice the following in the output window (if you have successfull deploy):













9-You can check successfull deployment by evaluating the Site Features. You will notice the new feature is installed and activated.














10-You can debug your code by attaching your project to a process. Go to "Debug" and select "Attach to process". Ensure that your site is open in Internet Explorer. Select the w3wp.exe process and click on attach. If you have more than one w3wp.exe process running you can select all of them and click on attach.



 11- You can step through the code and view field values of the item (note that my code segment in this screenshot is different, but it is only to illustrate the concept of debug.



12-If you upload a document you will notice that your custom field now contains the value of the "Name" field. You can hide or disable the control on the Edit Form to ensure users do not change the value.



13-If you have any questions please give me a shout !!!

0

Development Tip: C# Search for file in SharePoint document library using file name.

Recently I wanted to use the SPQuery object to locate an item (file) from a document library by searching on the filename.
I came across the following intersting limitation when using the SPQuery.
SharePoint store the filename of an in a native field on SPListItem called "Name".
One would therefore assume the "Name" field can be used in SPQuery.

Lets start with an example of SPQuery which works.
The following code segment search for a SPListItem (File) based on the Title of the file.

Microsoft.SharePoint.SPQuery query = new SPQuery();

StringBuilder strQuery = new StringBuilder();
String strFileName;
strFileName = "LocateThisFile"; //Hard-coded for illustration

strQuery.Append("<Where><Eq><FieldRef name='Title'><Value Type='Text'>");
strQuery.Append(strFileName);
strQuery.Append("</Value></Eq></Where>");

query.Query = strQuery.ToString();


SPListItemCollection mylistitemcollection = null;
mylistitemcollection = mylistitem.ParentList.GetItems(query);


foreach( SPItem loopitem in mylistitemcollection)
{
   loopitem.Delete();
   break;
}

Now, one would assume that the same can be done, and that by replacing the "Title" field with "Name", one can search for the item by filename.

Lets try the following: (same code segment but "Title" replaced with "Name")


Microsoft.SharePoint.SPQuery query = new SPQuery();
StringBuilder strQuery = new StringBuilder();
String strFileName;


strFileName = "LocateThisFile.docx"; //Hard-coded for illustration


strQuery.Append("<Where><Eq><FieldRef name='Name'><Value Type='Text'>");
strQuery.Append(strFileName);
strQuery.Append("</Value></Eq></Where>");

query.Query = strQuery.ToString();


SPListItemCollection mylistitemcollection = null;
mylistitemcollection = mylistitem.ParentList.GetItems(query);


foreach( SPItem loopitem in mylistitemcollection)
{
   loopitem.Delete();
   break;
}

Unfortunately the code segment above does not work and an exception is raised. (similiar to when one provide an invalid field name / field does not exist in list definition).


So, I did the following as a work around. I know this is not very robust and will definitely be the wrong approach for large document libraries. I am still looking into the problem, but for now the code below works well. I will post an update as soon as I am able to get the SPQuery to work with the "Name" field.



SPListItemCollection items = rw.Lists[properties.ListId].Items;

for (int i = 0; i < items.Count; i++)
{
   SPListItem looplistItem = items[i];


   if (looplistItem["Name"].ToString() == strCheckSealedFileName)
   {
      looplistItem.Delete();
      break;
   }
}






1

Development Tip: No SharePoint Site exists at the specified URL – Visual Studio Extensions for SharePoint

If you use Visual Studio Extensions for SharePoint to develop a webpart or feature (example: event handler) and you want to deploy the web part or feature from within Visual Studio, the VSE will try to deploy to port 80 on the local server as default.
If you want to move your code to another server or if you want to deploy your code to a web application on a different port you will get the following error when you deploy:


Error Message : “No SharePoint Site exists at the specified URL”



What you need to do is very straight forward:
In Visual Studio, go to Project , then Properties, select the Debug tab and change the "start browser with URL" to point your sharepoint website on the appropriate port.




 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Now your solution should deploy fine.

0

Beyond Technologies Session 1

Vision Casting Beyond Technology - Session 1
Today I presented a few ideas to construct a culture inside relate technologies with the aim to consistently Identify, Challenge, Workshop, Improve and Implement disciplines to help us deliver world class solutions.

Herewith the slides...
































0

Beyond Technologies


!! Exciting Stuff !!
I hosted a Boot Camp with my colleagues to
workshop our approach to delivering world-class solutions


0

Cool Technical Guides

Cool Technical Guides


SharePoint Products and Technologies: 2010 (Technical Preview) Developer Documentation
Contains:
White paper: Customizing the Ribbon in Windows SharePoint Services “14”
SDK: SharePoint Products and Technologies 2010 Managed Reference SDK (Technical Preview
Download From:
http://www.microsoft.com/downloads/details.aspx?FamilyID=94afe886-3b20-4bc9-9a0d-acd8cd232c24&displaylang=en

0

Dive into SharePoint 2010

Install a SharePoint 2010 Sandbox VM
Requirements
View the Microsoft Documentation on:
http://technet.microsoft.com/en-us/library/cc262485(office.14,printer).aspx

My Hardware: I used a VMWare Workstation to create a new Virtual Machine on my Dell Inspiron 1720 laptop.
XenServer or Virtual PC will also work.
The important thing to remember is that SP2010 only runs on 64bit operating systems.
If your host is 32bit OS you will have problems to install a 64bit VM. You will need to enable Virtualization Technology in the System Bios.
Check this article for tips.

Please allocate enough system resources to the VM. SP2010 is a hungry BEAST, so allocate 50gb virtual hdd space and at least 6GB ram.
6gb ram is ok but in a production environment will you need at least 8gb!!

Ok...ready to rumble..now install the software...

My Software:
Windows 2008 64 bit mode.
  
Download and Install the following software:

Microsoft SQL Server 2008 SP1
http://go.microsoft.com/fwlink/?LinkId=166490
or
Microsoft SQL Server 2005 SP3
http://go.microsoft.com/fwlink/?LinkId=166496

Microsoft Windows Server 2008 Standard SP2
http://go.microsoft.com/fwlink/?LinkId=166500
Microsoft .NET Framework 3.5 Service Pack 1
 http://go.microsoft.com/fwlink/?LinkId=131037
Microsoft SQL Server 2008 Express Edition Service Pack 1 http://go.microsoft.com/fwlink/?LinkId=166503
Microsoft "Geneva" Framework
 http://go.microsoft.com/fwlink/?LinkID=165752
Microsoft Sync Framework v1.0
http://go.microsoft.com/fwlink/?LinkID=141237&clcid=0x409
Microsoft Filter Pack 2.0
http://go.microsoft.com/fwlink/?LinkId=166504
Microsoft Chart Controls for Microsoft .NET Framework 3.5 http://go.microsoft.com/fwlink/?LinkID=141512
Windows PowerShell 2.0 CTP3
 http://go.microsoft.com/fwlink/?LinkId=165758
Microsoft SQL Server 2008 Native Client
http://go.microsoft.com/fwlink/?LinkId=166505
Microsoft SQL Server 2008 Analysis Services ADOMD.NET http://go.microsoft.com/fwlink/?LinkId=130651
Microsoft Silverlight 3.0
http://go.microsoft.com/fwlink/?LinkId=166506
ADO.NET Data Services v1.5 CTP2 for Windows Server 2008 SP2 http://go.microsoft.com/fwlink/?LinkId=158354
or
ADO.NET Data Services v1.5 CTP2 for Windows Server 2008 R2
http://go.microsoft.com/fwlink/?LinkId=159780

Okey.,... everything should now be in place !!!!

The first encounter with SP2010 is when you run the install wizard... and I was already impressed with the SP2010 Preparation Toolwhich show you if you had missed installation of any of the required software. (or if you have the wrong version in place)

Make use of the resources included in the "Prepare" section of the install wizard to ensure you familiarize yourself with the technical know-hows...


The installation Wizard will take you through a series of steps required to install .. please stick to "Single Server" installation and allow the configuration wizard to execute as normal.

After the install ...the Central Admin will open and allow you to create your first web app.
....after completing that step... you will have ... drums rolling...your first SP2010 Central Admin site up and running !!! Cool Bananas !

From here you can create new web apps, site collections and sites....

Enjoy !


0

SharePoint Development Tools

Microsoft Tools

SharePoint 2010 Related

Microsoft SharePoint Designer 2010 Beta (32-bit)
Download from: http://www.microsoft.com/downloads/details.aspx?FamilyID=82df15bd-16a5-460e-a7c4-22599c669bb1&displaylang=en

Microsoft SharePoint Designer 2010 Beta (64it)

Download from: http://www.microsoft.com/downloads/details.aspx?familyid=EEDA9AB1-AC53-4870-9E1C-38940343D677&displaylang=en

SharePoint Foundation 2010 Beta
Download from: http://www.microsoft.com/downloads/details.aspx?familyid=906C9F5A-6505-4EBA-BF24-95E423AC1703&displaylang=en

SharePoint Server for Internet Sites 2010 Beta
This is a Beta version of SharePoint Server 2010 for Internet Sites, Enterprise that enables extranet and internet sites using the full power of SharePoint Server 2010, the business collaboration platform for the Enterprise and the Web.
Download from: Http://www.microsoft.com/downloads/details.aspx?familyid=77195785-0282-4ACC-B6F4-7569E1011581&displaylang=en


SharePoint 2007
(section under construction)


Other Tools

Reflector:
.Net Reflector allows you to easily view, navigate, and search through, the class hierarchies of .NET assemblies, even if you don't have the code for them. With it, you can decompile and analyze .NET assemblies in C#, Visual Basic, and IL.
Download from: http://www.red-gate.com/products/reflector/