Tuesday, July 27, 2010

Adding Event Reciever Programmatically

I was wondering one evening how would you programmatically add an event reciever to a SharePoint list programmatically, and then it hit me.

Get the Event Reciever collection for the specific list and just add your own event reciever to that collection, as shown below:

SPEventReceiverDefinitionCollection eventRecievers = spRequestList.EventReceivers;

SPEventReceiverDefinition defFile = spRequestList.EventReceivers.Add();

defFile.Name = "Auto Provisioning - Mark Parker";
defFile.Type = SPEventReceiverType.ItemAdded;
defFile.SequenceNumber = 10000;
defFile.Assembly = "SP2010.MarkParker, Version, Culture, PublicKeyToken=";
defFile.Class = "SP2010.MarkParker.EventReciever";


I have now added the Item added event to the list in question. If you wish to add the any other event recievers to the list simply follow the same method and just change the Type of event.

Hope this helped you

Programmatically Adding SharePoint List

Adding list programmatically is pretty simple, all you need to do is grab the list collection for the current site and add a new list as shown below:

SPListCollection ListCollection = _spweb.Lists;
ListCollection.Add("Name", "Decription", SPListTemplateType.GenericList);

Its that simple you have just added a list called name with the description description to your sharepoint site.

Friday, July 23, 2010

SharePoint People Picker

Have you ever wondered if you can retrieve data like email, job title, etc, from the people picker control in SharePoint using Javascript? In short, yes it can be done and it is pretty simple.

Below is some javascript which accepts a parameter for the the position of the control, and returns the desired value.

function getEmailFromPoeplePicker(identifier)
var tags = document.getElementsByTagName('DIV');
for (var i = 0; i < tags.length; i++)
var tempString = tags[i].id;
if ((tempString.indexOf(identifier) > 0) && (tempString.indexOf('UserField_upLevelDiv') > 0))
var pplHTML = tags[i].innerHTML;
var valReturn = pplHTML.indexOf(">Email");
if (valReturn > 0)
pplHTML = pplHTML.substr(valReturn + 12);
var ValuePos1 = pplHTML.indexOf(">");
var ValuePos2 = pplHTML.indexOf("");
if (ValuePos1 > 0 && ValuePos2 > ValuePos1)
return (pplHTML.substring(ValuePos1 + 1, ValuePos2));
return null;

if you wish to return any other details from the people picker simply modify this line :

pplHTML = pplHTML.substr(valReturn + 12);

Change the substr value of 12 to the following :

29 For the mobile number
12 Gets the Job Title

This is only 3 of many examples of how it can be done, feel free to comment if you have found a better way.

Wednesday, July 21, 2010

Custom Consumer Web Part

Retrieving data from another web part by means of connections is pretty simple. In the code example I will show you how you can create your own consumer web part that will allow you to recieve a row of data from another web part, custom or out of the box.

Code Example :

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.Text;

namespace MarkAnthonyParker.ConsumsumerWebPart
public class ConsumsumerWebPart: WebPart

private IWebPartRow _provider;
private DataRowView _tableRow;

protected override void RenderContents(HtmlTextWriter writer)
if (_tableRow != null)
// Add method to use data

private void GetRowData(object tableRowData)
_tableRow = (DataRowView)tableRowData;

protected override void OnPreRender(EventArgs e)
if (_provider != null)
_provider.GetRowData(new RowCallback(GetRowData));

public void SetConnectionInterface(IWebPartRow provider)
_provider = provider;

public class TableConsumerConnectionPoint : ConsumerConnectionPoint
public TableConsumerConnectionPoint(System.Reflection.MethodInfo callbackMethod, Type interfaceType, Type controlType, string name,
string id, bool allowMultipleConnections)
: base(callbackMethod, interfaceType, controlType, name, id, allowMultipleConnections)

This code can be used for both SharePoint 2007 and SharePoint 2010 web parts.

Friday, July 16, 2010

Access denied - Dropping DLL into GAC

When you try and drop a DLL into the GAC in your Windows Server 2008 enviroment you get a error message saying Access Denied, even though you are an administrator on the machine in question.

Simple solution, UAC (User Access Control) is preventing you to do this as Windows thinks there is an attack on the GAC.

To rememdy this simply turn off UAC temporily, restart your computer and redeploy the Assembly into the GAC.

Monday, July 12, 2010

Attempted to use an object that has ceased to exist

Error : Attempted to use an object that has ceased to exist. (Exception from HRESULT: 0x80030102 (STG_E_REVERTED))

This error occurs when you are using the SPContext.Current.Web object and then trying to dispose it.

So, do not dispose of this object or other controls on your page will no longer be able to use it. Thus the error, attempted to use an object that has ceased to exist.

SharePoint will automatically dispose of the SPContext.Current.Web object once your page has completely loaded.

Tuesday, July 6, 2010

Uploading Documents to Document Library Programmatically

Uploading documents to a document library programmatically is pretty simple the code for it can be found below:

String filename = System.IO.Path.GetFileName(filepath);

SPFolder myLib = impersonatedWeb.Folders[documentLibraryName];

FileStream stream = File.Open(path, FileMode.Open);
SPFile file = myLib.Files.Add(filename, stream, true);


There you have it, its as that.

SharePoint ReadOnly Columns

Have you ever thought of changing SharePoint read-only columns? The created by, modified by columns, which always seem to be impossible to change, below is the code on how to change those columns:

SPList _pList = impersonatedWeb.Lists[ListName];

_pList.Fields["Created By"].ReadOnlyField = false;

//Update the relevant list item

Once you have updated the relevant list items you have to change the field back to Read Only mode, as shown below:

_pList.Fields["Created By"].ReadOnlyField = true;

SharePoint Timer Job

I have recently created a SharePoint Timer Job, I have deployed it as a feature and then activated it.

To my dismay I had forgot to insert a line of code. When I changed the code and re-deployed the timer job, the timer job still seemed to be using the same code as before.

After scratching my head for a while and realizing that I done everything short of restarting my server. I decided to restart the timer service. Once I did that everything worked.

Please note, you don’t have to reset “IIS” and you don’t need to restart your server. All you need to do is go to start, administrative tools, services and restart your SharePoint timer service.

If you have a farm installations make sure the new assembly is deployed to all the servers in the farm, and the SharePoint Timer service is restarted on each of the servers.