Thursday, November 28, 2013

Add a user to a custom group in SharePoint Group - A Function to serve the purpose


How do i add a user to a group using SharePoint Object Model.

The following function will help you to achieve this...

1. Get the loginname of the user and pass it to the following function
2. the following code adds the user to the Sharepoint Group called MYSite_MyGroup1.

public void AddUser(strloginName)
{
if (!string.IsNullOrEmpty(strloginName))
 {                                                                //Gets the Collection of Site Groups
                                SPGroupCollection objSPSiteGroups = objSPWeb.SiteGroups;
                               string  strCustomGroupName = "MYSite_MyGroup1";
                                if (objSPSiteGroups != null && !string.IsNullOrEmpty(strloginName))
                                {                                                                     
                                        // Adds the new user to the MYSite_MyGroup1 group
                                        objSPgpUserGroup = objSPWeb.SiteGroups[strCustomGroupName];

     // Make sure that the user is added to site . the following built in method makes sure it is added to the site
                                        objnewUser = objSPWeb.EnsureUser(strloginName);                                    


              //Add the user to group                         
                                        objSPgpUserGroup.AddUser(objnewUser);
                                        objSPgpUserGroup.Update();                                 
                                   

                                 }
 }
}


And thats it...

Monday, November 25, 2013

How to remove an event Handler from a list ? A method to detach the event handler

In my previous post i explained about a method to add/attach an event handler to a list.

Now what would you do to remove an event handler from a list ?

The following function would help you to achieve the same in sharepoint..

Just like in the adding event handler post , create the following method.



  private void RemoveHandler(SPList objlist, string strrecieverName)
        {
            try
            {
              
                Guid objGuid = new Guid();
                SPEventReceiverDefinition objEvent = objlist.EventReceivers.Cast<SPEventReceiverDefinition>().FirstOrDefault(l=>string.Compare(l.Name,strrecieverName,true)==0);
                if (objEvent != null)
                {
                    objGuid = objEvent.Id;
                }

                if (objGuid.CompareTo(System.Guid.Empty) != 0)
//Deletes the eventHandler object
                    objEvent.Delete();
            }
            catch (Exception ex)
            {
                ExceptionPolicy.HandleException(ex, CommonConstants.ITI_EXCEPTION);
            }
        }
----------------------------------------------------------------------------------------------------------------------------------------------


Now call the RemoveHandler() method from the feature deactivating method of Reciever.cs .

This will remove the adding and deleting event handler from the test list respectively.


   public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWeb objweb = (SPWeb)properties.Feature.Parent;
            List<SPList> objspLists = new List<SPList>();
            try
            {
                SPList objTestList = objweb.Lists.Cast<SPList>().FirstOrDefault(l => string.Compare(l.Title, "TestList", true) == 0);
       
                if (TestList!= null)
                {
                   //Remove the adding and deleting event handler from the test
                    RemoveHandler(objTestList , "AddingEventHandler");
                    RemoveHandler(objTestList , "DeletingEventHandler");
                  
                }
            }
            catch (Exception ex)
            {
                ExceptionPolicy.HandleException(ex, CommonConstants.ITI_EXCEPTION);
            }

        }

-----------------------------------------------------------------------------------------------------------------------------------------------------------------


How to attach an event handler to a SharePoint List using C#? a simple reusable method for attaching an event handler

Creation of event handler requires creation of an event receiver class in SharePoint .  For this we would  need to create a feature receiver from the already built in template of visual studio.  Once the receiver is created we can use the following code to attach the event handler to any list.


The following is the code to attach the event handler to the list

Reciever.cs

 private void AddHandler(SPList objList, string strRecieverName, int iseqno, SPEventReceiverType eventType)
        {
            try
            {
                SPEventReceiverDefinitionCollection objEventDefColl = objList.EventReceivers;
                ////Set the values for the Definition object 
                Assembly objAssembly = Assembly.GetExecutingAssembly();
                string strAssemblyName = objAssembly.FullName;
               
// MyCustomClass.cs" is the Name of the class in which the logic is writen to handle various events like adding,updating,deleting
                 string strClassName = "MyCustomClass.cs";
      
                string strReceiverName = strRecieverName;
                string strDefData = "Data";
                int iSequenceNo = iseqno;
                ////Create the Definition object 
                SPEventReceiverDefinition oEventDef = objEventDefColl.Add();
                ////Set the properties
                oEventDef.Name = strReceiverName;
                oEventDef.Assembly = strAssemblyName;
                oEventDef.Class = strClassName;
                oEventDef.Data = strDefData;
                oEventDef.SequenceNumber = iSequenceNo;
                oEventDef.Type = eventType;
                oEventDef.Update();
            }
            catch (Exception ex)
            {
                ExceptionPolicy.HandleException(ex, CommonConstants.ITI_EXCEPTION);
            }
        }


---------------------------------------------------------------------------------------------------------------------------------------------

Now in the reciever.cs file just call the method AddHandler inside the FeatureActivated Mehod.

This will enable the event handler to be attached to the TestList as soon as the feature is activated on thesite


  public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPWeb objweb = (SPWeb)properties.Feature.Parent;
                SPList objTestList= objweb.Lists.Cast<SPList>().FirstOrDefault(l => string.Compare(l.Title,TestList, true) == 0);
                //Attach the ItemAdded event and the ItemDeletingEvent
                if (objTestList!= null)
                {
                   
                    AddHandler(objTestList, "AddingEventHandler", 10003, SPEventReceiverType.ItemAdding);
                    AddHandler(objTestList, "DeletingEventHandler", 10002, SPEventReceiverType.ItemDeleting);
                                    
                    
                }
            }
            catch (Exception ex)
            {
                ExceptionPolicy.HandleException(ex, CommonConstants.ITI_EXCEPTION);
            }

        }

--------------------------------------------------------------------------------------------------


Happy SharePointing again :)


Sunday, November 24, 2013

what is the maximum file size limit in Sharepoint 2013 ?

what is the maximum file size limit in SharePoint 2013 ?

Its 2GB.

Yes the maximum size of file should not exceed 2 GB.

Rename a Web Application in Sharepoint : A solution using powershell

Here is a small script that will enable you all to change a SharePoint Web Application name . We can use the following SharePoint Pow...