Monday, April 12, 2010

Sharepoint 3 -Event Reciever Handler

There are many resources explained about Event Receiver Handler in Sharepoint. Let's take the SPItemEventReciever as an example: (I was planning to synchronize two list in sharepoint using Workflow, but for item deleting/deleted, the item cannot trigger an WF, in this case I wrote code of event reciever)

Here I wrote what I did in my project.

Step 1. Write the class inherited from SPItemEventReciever.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace DeleteReference
{
public class DeleteItem: SPItemEventReceiver
{
public override void ItemDeleted(SPItemEventProperties properties)
{
SPSite site= new SPSite("http://myserver:3000/");
using (SPWeb web = site.OpenWeb())
{
SPList list = web.GetList("/Jerry/Lists/Task2");
SPListItem item = list.Items.Add();
item["Title"] = "A";
//item["Title"] = properties.ListItem["Title"];
item.Update();
}
}

public override void ItemDeleting(SPItemEventProperties properties)
{

using (SPWeb web = properties.OpenWeb().ParentWeb)
{
SPList list = web.GetList("/Jerry/Lists/Task2");
SPListItem item = list.Items.Add();
item["Title"] =properties.ListItem["Title"];
//item["Title"] = properties.ListItem["Title"];
item.Update();
}
}
public override void ItemAdded(SPItemEventProperties properties)
{

SPListItem item = properties.ListItem;
this.DisableEventFiring();
item["Release"] = "AB";
item.Update();
this.EnableEventFiring();
/*SPSite site = new SPSite("http://myserver:3000/");
using (SPWeb web = site.OpenWeb())
{
SPList list = web.GetList("/Jerry/Lists/Release1");
SPListItem item = list.Items.Add();
item["Title"] = "C";
//item["Title"] = properties.ListItem["Title"];
item.Update();
}*/
}

}
}

Step 2.

Use the strong named signing and register it in GAC.

Step 3.
Write the feature.xml and Element.xml file in the folder(Create DeleteReference folder under the FEATURES folder)

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\DeleteReference

feature.xml:
< Feature Scope="Web"
Title="Deleting Event Handler"
Id="8CDFAE41-98F9-4139-9546-A7C0B7D3C1FE"
xmlns="http://schemas.microsoft.com/sharepoint/">
< ElementManifests>
< ElementManifest Location="Element.xml"/>
< /ElementManifests>
< /Feature>

Element.xml

< Elements xmlns="http://schemas.microsoft.com/sharepoint/">
< Receivers ListTemplateId="107">
< Receiver>
< Name>DeleteReference< /Name>
< Type>ItemAdded< /Type>
< SequenceNumber>10000< /SequenceNumber>
< Assembly>DeleteReference, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ef537f21ff0cf29f< /Assembly>
< Class>DeleteReference.DeleteItem< /Class>
< Data>< /Data>
< Filter>< /Filter>
< /Receiver>
< /Receivers>

< Receivers ListTemplateId="107">
< Receiver>
< Name>DeleteReference< /Name>
< Type>ItemDeleted< /Type>
< SequenceNumber>10000< /SequenceNumber>
< Assembly>DeleteReference, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ef537f21ff0cf29f< /Assembly>
< Class>DeleteReference.DeleteItem< /Class>
< Data>< /Data>
< Filter>< /Filter>
< /Receiver>
< /Receivers>

< Receivers ListTemplateId="107">
< Receiver>
< Name>DeleteReference< /Name>
< Type>ItemDeleting< /Type>
< SequenceNumber>10000< /SequenceNumber>
< Assembly>DeleteReference, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ef537f21ff0cf29f< /Assembly>
< Class>DeleteReference.DeleteItem< /Class>
< Data>< /Data>
< Filter>< /Filter>
< /Receiver>
< /Receivers>

< /Elements>

Notice the ListTemplateID is also easy to be found in internet resources, for example:
GenericList 100
DocumentLibrary 101
Survey 102
Links 103
Announcements 104
Contacts 105
Events 106
Tasks 107
DiscussionBoard 108
PictureLibrary 109

For us we want our feature register to a task list, so we use 107.


Step 4.

Install the features to the subsite where the list is:

pushd %programfiles%\common files\microsoft shared\web server extensions\12\bin

::stsadm -o deactivatefeature -filename DeleteReference\feature.xml -url http://myserver:3000/Jerry/List/Task1
::stsadm -o uninstallfeature -filename DeleteReference\feature.xml


stsadm -o installfeature -filename DeleteReference\feature.xml

stsadm -o activatefeature -filename DeleteReference\feature.xml -url http://myserver:3000/Jerry/List/Task1

iisreset

Tuesday, March 30, 2010

Sharepoint 2 - Cascaded Lookup Dropdowns

there is a javascript components we can use directly, follow the instruction of this link,http://www.sharepointboris.net/js/spcd/

1. Download the spcd.js from codeplex: http://spcd.codeplex.com/

2. Upload our spcd.js to our documents library

3. Build the list show the relationship with the dropdown item and child nodes, such as: product-newspapers list, product-version list.

4. In the item page, add 2 paramters to the URL: PageView=Shared&ToolPaneView=2 and add the Content Editor Webparts

5. Specify the javascript code in the source code editor, like:
< script type="text/javascript" src="[paste-javascript-url-here]">
var ccd1 = new cascadeDropdowns(ParentDropDownTitle, ChildDropDownTitle, Child2ParentFieldIntName, ChildListNameOrGuid, ChildLookupTargetField);
< /script>

For example:
< script type="text/javascript">
var ccd1 = new cascadeDropdowns("Account", "Branch", "Account", "Branches", "Title");
< /script>

Sharepoint 1-The Way to find the GUID of a List

•Copy the Url from the browser address bar into Notepad. It will look something like:
http://moss2007/ProjectX/_layouts/listedit.aspx?List=%7B26534EF9%2DAB3A%2D46E0%2DAE56%2DEFF168BE562F%7D


for the list url:



Delete everying before and including “List=”.
Change “%7B” to “{”
Change all “%2D” to “-“
Chnage “%7D” to “}”

Monday, March 15, 2010

Convert String to integer

int i = Convert.ToInt32(str);

int i = int.Parse(str);

int i;
int.TryParse(str, out i);

But for the third method, i cannot be property. It will come out an error:
A property or indexer may not be passed as an out or ref parameter.

Monday, March 1, 2010

Ref and out parameters

ref= in and out. Both allow the member functions to change the values of the parameters. So the difference between ref and out is ,for out, the variable passed as the parameter doesn’t have to have a known value before the call.

Here are an example:

static void Main()
{
string value1 = "cat";
SetString1(ref value1);
Console.WriteLine(value1);

string value2; // Unassigned string
SetString2(out value2);
Console.WriteLine(value2);
}

static void SetString1(ref string value)
{
value = "dog";
}
static void SetString1(out string value)
{
value = "bird";
}

Output:
dog
bird

Tuesday, February 23, 2010

Parallel For and Parallel ForEach

Parallel Extension can get multi-threading code simplified.
In order to use parallel, we need to download the parallel extension from the following link:
http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx

Reference System.Threading dll.
Include the following namespaces:
using System.Threading;
using System.Threading.Tasks;

For non-parallel(sequential) loop, we have code:
for (int i = 0; i < N; i++)
{
results[i] = Compute(i);
}
When use parallel, we have:
Parallel.For(0, N, delegate(int i) {
results[i] = Compute(i);
});
We can use lambda expressions to simplify the code:
Parallel.For(0, N, delegate(int i) {
results[i] = Compute(i);
});
We can also have:
Parallel.For(0, size1, i =>
{
for (int j = 0; j < size2; j++)
{
Compute(i, j);
}
});
or:
Parallel.For(0, size1, i =>
{
Parallel.For(0, size2, j =>
{
Compute(i, j);
});
});

Here we write a sample code:
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ParallelTesting
{
class Program
{
static void Main(string[] args)
{
int[,] results=new int[5,4];
Parallel.For(0, 5, i =>
{
Parallel.For(0, 4, j =>
{

results[i, j] = Compute(i, j);
Console.WriteLine(results[i, j]);
});
});

}
private static int Compute(int i,int j)
{
return i * i+j;
}
}
}

Similarly, Parallel ForEach:

For a sequential loop:

foreach(item c in CollectionA)
{
Compute(c);
}

We can parallelized as:

Parallel.ForEach(CollectionA, delegate(item c)
{
Compute(c);
});

Use lambda expressions:

Parallel.ForEach(CollectionA, c =>
{
Compute(c);
});

Wednesday, January 27, 2010

Add the assembly reference to web.config File

< configuration>
< system.web>
< compilation>
< assemblies>
< add assembly="< AssemblyName>, Version=< Version>, Culture=< Culture>, PublicKeyToken=< PublicKeyToken>"/>
< /assemblies>
< /compilation>
< /system.web>
< /configuration>