Tuesday, March 11, 2014

How to get the site collection URL in the masterpage of a SharePoint 2013 site or subsites using Javascript in masterpage

There might be times when you really want to get the URL of the site collection within the master page of sharepoint 2013 sites.

well you can achieve it by using the below javascript line .

Inorder to add script in masterpage , we have to follow the conventional and well known method of the script tag inside the body of HTML.

now within the script tag just copy paste the following function

function getSiteCollectionsURL()

{

var sitecollectionURL = window.location.protocol + "//" + window.location.host +_spPageContextInfo.siteServerRelativeUrl

return sitecollectionURL;

}

the function can be called from anywhere inside the masterpage according to your needs.. it will return the site collection url no matter where you are , ie , whether you are in the subsite or the site collection or the nested subsite.

Thursday, March 6, 2014

Read a list column value using SP services in SharePoint

How can i read  a column value value of a share point list using Jquery and SP services

This can be achieved by the following java script function .

Make sure in your JS page you have refered the jquery and spservices link
//read the tiltle field value of ABC list
function ReadColumnValue()
{

var myvalue=null;
var strViewfields = "<ViewFields><FieldRef Name='Title'/></ViewFields>";
$().SPServices({
webURL: strSiteURL,
            operation: "GetListItems",
            async: false,
            listName: "ABC",

            completefunc: function (xData, Status) {
                $(xData.responseXML).SPFilterNode("z:row").each(function () {
               
if($(this).attr("ows_title")!= undefined)
{

       myvalue=$(this).attr("ows_title");


}
                });
            }

        });



the variable myvalue will containt eh list column value of the tilte field of the AB list




How to make sure if a particular SharePoint Group Exists or Not?

Inorder to know if a particular group exists in sharepoint group collection , just use the following function..

 private bool GroupExists(SPGroupCollection objgroups, string strName)
        {
            const string NAME = "Name";
            bool isGroupExists = false;
            if (string.IsNullOrEmpty(strName) ||

                (strName.Length > 255) ||

                (objgroups == null) ||

                (objgroups.Count == 0))
            {

                isGroupExists = false;
            }

            else
            {
                XDocument doc = XDocument.Parse(objgroups.Xml);
                isGroupExists = doc.Descendants().Where(g => g.Attribute(NAME) != null).Where(g => g.Attribute(NAME).Value.Equals(strName, StringComparison.InvariantCultureIgnoreCase)).Count() > 0;
            }
            return isGroupExists;

        }


The function would return true if the group exists in group collection of sharepoint..


You just have to call the function as shown below :



                SPGroupCollection objSiteGroups = YOURWEBOBJECT.SiteGroups;
                if (GroupExists(objSiteGroups, "YOUR GROUP NAME"))
                {
                   // Do all that you want with the group coz the group does exists
                }
Happy sharepointing :)


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...