Sunday 12 January 2014

Add and Remove Event Receiver through Feature SharePoint 2010

1. Add Event Receiver using Visual Studio( Here, I added list leve ItemUpdated event receiver
     Solution -->Add -->Add New Item


2.Add feature reciever and add  the below code

public class FeatureEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb spWeb = properties.Feature.Parent as SPWeb;
            try
            {
                using (SPSite siteCollection = new SPSite(spWeb.Url))
                {
                    using (SPWeb web = siteCollection.OpenWeb())
                    {
                        SPList list = web.Lists["ListName"];
                        SPEventReceiverDefinitionCollection spEventReceiverDefinitionCollection = list.EventReceivers;
                        if (!isEventAlreadyAttached(spEventReceiverDefinitionCollection, "AssemblyName"))
                        {
                            SPEventReceiverType spEventReceiverType = SPEventReceiverType.ItemUpdated;
                            spEventReceiverDefinitionCollection.Add(spEventReceiverType, " AssemblyName ", "EventReceiverClassName");
                            list.Update();
                            web.Update();
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                //Log you message
            }
            finally
            {
                spWeb.Dispose();
            }
          
        }
        private bool isEventAlreadyAttached(SPEventReceiverDefinitionCollection spEventReceiverDefinitionCollection, string myAssembly)
        {
            bool eventReceiverAttached = false;
            foreach (SPEventReceiverDefinition spEventReceiverDefinition in spEventReceiverDefinitionCollection)
            {
                if (spEventReceiverDefinition.Assembly.Contains(myAssembly))
                {
                    eventReceiverAttached = true;
                    break;
                }
            }
            return eventReceiverAttached;
        }

        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
           
            SPWeb spWeb = properties.Feature.Parent as SPWeb;
            try
            {
                RemoveEventReceiver(spWeb.Url);
              
            }
            catch
            {
                return;
            }
            finally
            {
                spWeb.Dispose();
            }
        }

        private void RemoveEventReceiver(string siteUrl)
        {
            using (SPSite siteCollection = new SPSite(siteUrl))
            {
                using (SPWeb web = siteCollection.OpenWeb())
                {
                    List<Guid> receiversToRemove = new List<Guid>();
                    SPList list = web.Lists["ListName"];
                    SPEventReceiverDefinitionCollection spEventReceiverDefinitionCollection = list.EventReceivers;
                    for (int i = 0; i < spEventReceiverDefinitionCollection.Count; i++)
                    {
                        if (spEventReceiverDefinitionCollection[i].Assembly.Contains("AssemblyName"))
                        {
                            receiversToRemove.Add(spEventReceiverDefinitionCollection[i].Id);
                        }
                    }
                    if (receiversToRemove.Count > 0)
                    {
                        foreach (var guid in receiversToRemove)
                        {
                            list.EventReceivers[guid].Delete();

                        }
                        list.Update();
                        web.Update();
                    }
                }
            }
        }
    }

Note: Replace ListName and AssemblyName details.


Monday 6 January 2014

Update SharePoint 2010 Farm Credentials

In SharePoint 2010, for SharePoint single server install – a common task that you have to do is to update the SharePoint Farm credentials  when ever the password changes.

Below are the two simple steps to update Farm Credentials:
1. Go to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\Bin folder using cmd.exe(open it as administrator).

2. Run this command using your credentials.

stsadm –o updatefarmcredentials –userlogin DOMAIN\username –password $password$

Note: Please type the whole command by your own including each and everything. sometimes copy  & paste leads to text encode.


Bug Fix:
If your SQL Server service is running under your windows credentials. please make sure that credentials updated manually. otherwise you will get below error.
Error: Value cannot be null. Parameter name: farm
Fix: 
1)  Just go to services.msc and change the sql server service password to your current windows password,
2) Restart the sql server service.
3) Run the above command.