Monday, April 27, 2009
How to debug event handler
2. Run-> cmd-> subst z: c:\windows\asembly
3. copy MyEventhandler.dll and MyEventhandler.pdb to z:\GAC_MSIL\MyEventhandler\1.0.0.0__publickeytoken
4. start IIS
5. go to site where the list is (!importan! before step 6. so that w3wp.exe appears in the list of processes)
6. VisualStudio MyEventhandler.sln -> Debug-> Attach to Process -> w3wp.exe (UserName) Server\appPool_server (or something similar).
7. Now you can debug!!
Saturday, April 25, 2009
How to get Internalname of ListItem fields
private string GetFieldsInternalName(SPItemEventProperties properties) { string ret = ""; for (int i = 0; i < properties.ListItem.Fields.Count; i++) { ret += " [" + i + "]: " + properties.ListItem.Fields[i].InternalName.ToString(); } return ret; } |
ItemAdding VS ItemAdded
ItemAdding - HttpContext namespace MyEventHandler { public class MyAction :SPItemEventReceiver { HttpContext curr; public MyAction() { curr = HttpContext.Current; } public override void ItemAdding(SPItemEventProperties properties) { base.ItemAdding(properties); if (properties.ListTitle.Equals("Name of the CustomList")) { if (curr != null) { ret = curr.User.Identity.Name.Trim(); } } } } } ItemAdded - HttpContext /? |
ItemAdding - Update ListItem namespace MyEventHandler { public class MyAction : SPItemEventReceiver { public MyAction() { } public override void ItemAdding(SPItemEventProperties properties) { base.ItemAdding(properties); if (properties.ListTitle.Equals("Name of the CustomList")) { //properties.ListItem is NULL properties.AfterProperties["internalname of the field"] = "some text"; } } } } ItemAdded - Update ListItem namespace MyEventHandler { public class MyAction : SPItemEventReceiver { public MyAction() { } public override void ItemAdded(SPItemEventProperties properties) { base.ItemAdding(properties); if (properties.ListTitle.Equals("Name of the CustomList")) { properties.ListItem["internalname of the field"] = "some text"; properties.ListItem.Update(); } } } } |
ItemAdding - Attachment /? ItemAdded - Attachment namespace MyEventHandler { public class MyAction : SPItemEventReceiver { public MyAction() { } public override void ItemAdded(SPItemEventProperties properties) { base.ItemAdding(properties); if (properties.ListTitle.Equals("Name of the CustomList")) { string filename = "Name of the file for attachment"; FileStream fs = File.OpenRead("Address of the file for attachment"); byte[] cv = new byte[fs.Length]; fs.Read(cv, 0, cv.Length); string desturl = properties.ListItem.Attachments.UrlPrefix + filename; properties.ListItem.Attachments.Add(desturl, cv); properties.ListItem.Update(); } } } } |
SendMail whit smtp.gmail.com
private void SendMailWithSmtpGmail() { MailMessage mail = new MailMessage(); NetworkCredential cred = new NetworkCredential("your gmail account", "your password"); mail.To.Add("whatever@something"); mail.Subject = "Some text"; mail.From = new System.Net.Mail.MailAddress("whatever@something"); mail.Body = "Send @ " + DateTime.Now.ToString(); SmtpClient smtp = new SmtpClient("smtp.gmail.com"); smtp.UseDefaultCredentials = false; smtp.EnableSsl = true; smtp.Credentials = cred; smtp.Port = 587; smtp.Send(mail); } |
Sending Mail whit attachment
private void SendMailWithAttachment() { string mail_from = "your account"; string mail_to = "whatever@something"; string mail_smtp_server = "smtp.someserver"; MailMessage message = new MailMessage(); MailAddress sender = new MailAddress(mail_from); message.From = sender; message.Subject = "Some text"; message.Body = "Massage"; // attachment FileStream fs = File.OpenRead("destination of the file"); byte[] cv = new byte[fs.Length]; fs.Read(cv, 0, cv.Length); MemoryStream ms = new MemoryStream(cv); Attachment attach = new Attachment(ms, "filename"); message.Attachments.Add(attach); message.To.Add(mail_to); SmtpClient smtp = new SmtpClient(mail_smtp_server); smtp.Send(message); } |
How to get sAMAccountName from Active Directory
using System.DirectoryServices; private string GetsAMAccountName() { string domuser = PContext.Current.Web.CurrentUser.LoginName.Trim(); domuser = domuser.Substring(domuser.IndexOf(@"\")+ 1); string ret = ""; //init ldap call DirectoryEntry dentry = new DirectoryEntry("LDAP://server"); //search ldap DirectorySearcher dsearcher = new DirectorySearcher(dentry); dsearcher.PageSize = 1000; dsearcher.Filter = ("(&(|(objectClass=person)(objectClass=user))(sAMAccountName=" + domuser + "))"); // find users foreach (System.DirectoryServices.SearchResult sres in dsearcher.FindAll()) { DirectoryEntry de = sres.GetDirectoryEntry(); if(de.Properties["employeeID"] != null && de.Properties["employeeID"].Value != null) { string employee = de.Properties["employeeID"].Value.ToString().Trim(); System.DirectoryServices.PropertyCollection props = de.Properties; ret += props["sAMAccountName"][0]; } } return ret; } |
How to create a event handler
1. Create a new project in Visual Studio by clicking File, pointing to New, and
thenclicking Project.
2. In the New Project dialog box, select Visual C# in the Project types box, select ClassLibrary in the Templates box, type MyEventHandler in the Name box,and then click OK.
3. In Solution Explorer, select MyEventHandler, and click Add Reference on the Project menu.
4. In the Add Reference dialog box, select Microsoft.SharePoint on the Browse tab and then click OK. (C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\Microsoft.SharePoint.dll)
5. In the Code Editor, import the Microsoft.SharePoint namespace as follows.
using Microsoft.SharePoint: |
6. Change the name of the class to MyAction and make it inherit from the SPItemEventReceiverclass, as follows.
public class MyAction : SPItemEventReceiver |
7. Add some code within the class to override the ItemDeleting method.
public override void ItemDeleting(SPItemEventProperties properties) { properties.Cancel = true; properties.ErrorMessage = "Deleting items from " + properties.RelativeWebUrl + " is not supported."; } |
8. In Solution Explorer, right-click the MyEventHandler node, and then click Properties.
9. In the Properties dialog box, click the Signing tab, select Sign the asembly, select Choose a strong name key file, and then click <New…>.
10. In the Create Strong Name Key dialog box, type MyEventHandler.snk in the Key file name box, optionally specify a password for the key, and then click OK.
11. To build the project, click Build Solution on the Build menu, or press CTRL+SHIFT+B.
12. Find the \MyEventHandler\bin\Debug folder in the Visual Studio Projects folder, and drag the MyEventHandler.dll file to C:\WINDOWS\assembly to place the DLL in the global assembly cache.
- - - - - - - - - Add the event handler as a Windows SharePoint Services Feature
1. Create a folder in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES called MyEventHandler.
2. Create a Feature.xml Files file in this folder like the following that identifies the Feature and its element manifest file and sets the Feature scope to Web site.
<Feature Scope="Web" Title="Deleting Event Handler" Id="GUID" xmlns="http://schemas.microsoft.com/sharepoint/"> <ElementManifests> <ElementManifest Location="Elements.xml"/> </ElementManifests> </Feature> |
3. To replace the GUID placeholder in the previous Id attribute, generate a GUID by running guidgen.exe located in C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\guidgen.exe.
4. Create an Elements.xml file in the MyEventHandler folder that identifies the assembly, class, and method to implement as the event handler. This example applies the event handler to all announcements lists of a site, as specified by the ListTemplateId attribute. For the IDs of other default Windows SharePoint Services list template types, see the Type attribute description of the ListTemplate element.
<Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Receivers ListTemplateId="100"> <Receiver> <Name>MyEventHandler</Name> <Type>ItemDeleting</Type> <SequenceNumber>10000</SequenceNumber> <Assembly>MyEventHandler, Version=1.0.0.0, Culture=neutral,PublicKeyToken=key</Assembly> <Class>MyEventHandler.MyAction</Class> <Data></Data> <Filter></Filter> </Receiver> </Receivers> </Elements> |
5. To get the Public Key Token of the assembly, in Windows Explorer find the MyEventHandler.dll file in the C:\WINDOWS\assembly, right-click the file, click Properties, and on the General tab of the Properties dialog box, select and copy the token.
6. At a command prompt, navigate to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN on the local drive, and type each of the following commands to install the Feature in the deployment, activate the Feature on a specified
subsite, and reset Microsoft Internet Information Services (IIS) so that the changes take effect:
stsadm -o installfeature -filename MyEventHandler\Feature.xml stsadm -o activatefeature -filename MyEventHandler\Feature.xml -url http://Server/Site/Subsite iisreset |
Friday, April 24, 2009
How to access sharepoint website in windows service
using (SPSite site = new SPSite("http://Server/Site/")) { try { using (SPWeb web = site.OpenWeb()) { //some code } }catch(Exception ex) { //do something whit exception } } |
Where is the ...
Microsoft.SharePoint.dll:
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\Microsoft.SharePoint.dll
New GUID (guidgen.exe):
C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\guidgen.exe.
CMD:
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN
PublicKey Token:
To get the Public Key Token of the assembly, in Windows Explorer find the MyEventHandler.dll file in the C:\WINDOWS\assembly, right-click the file, click Properties, and on the General tab of the Properties dialog box, select and copy the token.
Installing EventHandler:
stsadm -o installfeature -filename dirwhereeventis\Feature.xml
stsadm -o activatefeature -filename dirwhereeventis\Feature.xml -url http://Server/Site/Subsite
iisreset
*dirwhereeventis = C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATES\FEATURES\nameofdir
ListTemplateId and Events
ItemAdding
ItemAttachmentAdded
ItemAttachmentAdding
ItemAttachmentDeleted
ItemAttachmentDeleting
ItemCheckedIn
ItemCheckedOut
ItemCheckingIn
ItemCheckingOut
ItemDeleted
ItemDeleting
ItemFileConverted
ItemFileMoved
ItemFileMoving
ItemUncheckedOut
ItemUncheckingOut
ItemUpdated
ItemUpdating