Thursday, 12 February 2015

SharePoint 2013: How to get current user name using spservices.js

How to get current user name using spservices.js:

Then download the SPUtility.js file from http://spservices.codeplex.com/ and jquery-1.11.2.min.js file as well.

<script type="text/javascript" src="/Style Library/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="/Style Library/spservices.js"></script>

$(document).ready(function() {      
        var currentAbsoluteUrl = window.location.protocol + "//" + window.location.host;
        var currentUserName= $().SPServices.SPGetCurrentUser({
        webURL:currentAbsoluteUrl,
        fieldName: "Title",
        debug: false
    });
  alert('User Name:'+currentUserName);
});

How to use SPUtility.js file in SharePoint list form for Hide/Show the columns, set a DropDown Changed event and RadioButton changed event using Jquery

How to use SPUtility.js file in SharePoint list form for Hide/Show the columns, set a DropDown Changed event and RadioButton changed event using Jquery:

For this, you should add a content editor webpart inside newform.aspx, where you want to show/hide the list columns. 
Then download the SPUtility.js file from http://sputility.codeplex.com/ and jquery-1.11.2.min.js file as well. we have to add a below script inside content editor WebPart. then it will works.

<script type="text/javascript" src="/Style Library/sputility/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="/Style Library/sputility/sputility.min.js"></script>

<script type="text/javascript">


function OnStatusFieldDropdownChange(spfield) {
    var statusFieldValue = spfield.GetValue(); 
           
    if(statusFieldValue === '4-Closed') {
        SPUtility.GetSPField('Resolved By').Show();
       
        
    } else{ 
        SPUtility.GetSPField('Resolved By').Hide();
        
    }   
}

function OnRadioButtonsChange() {
    var statusFieldValue = SPUtility.GetSPField('Reference Type').GetValue();
    if(statusFieldValue === 'File Attachment') {
        SPUtility.GetSPField('Revision Date').Show();
        SPUtility.GetSPField('URL').Hide();
    } else{
        SPUtility.GetSPField('Revision Date').Hide();
        SPUtility.GetSPField('URL').Show();
    }
}

$(document).ready(function() {


SPUtility.GetSPField('Title').Show();
SPUtility.GetSPField('Resolved By').Hide();
SPUtility.GetSPField("Title").SetValue('Sample Value').MakeReadOnly();

//Set DropDown chnaged event
var statusFieldValue = SPUtility.GetSPField('Status');
 $(statusFieldValue.Dropdown).on('change', function() { 
        OnStatusFieldDropdownChange(statusFieldValue);
    });

//Set Radiobutton changed event
var RreferenceTypeRadio = SPUtility.GetSPField('Reference Type');
$(RreferenceTypeRadio.RadioButtons).each(function(index, obj) {
        var radioButton = obj.value;
        $(radioButton).change(OnRadioButtonsChange);
    });
      
});
</script>

SharePoint2013: Getting user(s) from people picker control and set value to people picker column in SharePoint listitem

Getting user(s) from people picker control and set value to people picker column in SharePoint listitem:

                            SPWeb mySite = SPContext.Current.Web;
                            SPListItemCollection listItems = mySite.Lists["myList"].Items;
                            SPListItem item = listItems.Add();

                           //getting value from People picker control                            
                            string[] UsersSeperated = pplEditor.CommaSeparatedAccounts.Split(',');
                            SPFieldUserValueCollection UserCollection = new SPFieldUserValueCollection();
                            foreach (string UserSeperated in UsersSeperated)
                            {
                                mySite.EnsureUser(UserSeperated);
                                SPUser User = mySite.SiteUsers[UserSeperated];
                                SPFieldUserValue UserName = new SPFieldUserValue(mySite, User.ID, User.LoginName);
                                UserCollection.Add(UserName);
                            }
                            item["people"] = UserCollection;
                            item.Update();

SharePoint 2013: Sending Mail in SharePoint using SPUtility

Sending Mail in SharePoint using SPUtility Method:

If you want to send a mail from SharePoint from server side. use below function:

            StringDictionary headers = new StringDictionary();
            headers.Add("to", strTo);
            headers.Add("cc", strCC);
            headers.Add("bcc", strbcc);
            headers.Add("from", strFrom);
            headers.Add("subject", strSubject);
            headers.Add("content-type", "text/html");
            StringBuilder mailbody = new StringBuilder();
            mailbody.AppendLine("The following request has been assigned to you:<br/>");
            mailbody.AppendLine("<br/>");
            mailbody.AppendLine("clicking on the following link: <a href='" + Link + "'>" + Request Name + "</a><br/>");
            SPUtility.SendEmail(web, headers, mailbody);

SharePoint 2013: Show Modal Popup Window in Application Page

Show Modal Popup Window in Application Page

I want to show the modal Popup window from my application page, for that we need to call the below JS file inside your application page.

<script type="text/javascript" src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>

And the call the Jquery function for modal Popup your application page as below we called:

<script type="text/javascript">
        function showModalPopup() {
            alert('Bala testing on this');
            try {
                var options = {
                    width: 630,
                    height: 500,
                    title: "Edit SR Request",
                    bgcolor: '#000000',
                    url: "/_layouts/15/GridVewSort/Seekopinion.aspx?Reqno=USA&list=countries",
                }
            }
            catch (e) {
            }
            SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options);
            return false;
        }
</script>

If you want to cancel the modal Popup window without refresh your screen. you can use below script in cancel button client click:

<asp:Button ID="btnCancel" runat="server" Text="Cancel"  CssClass="btnclass" OnClientClick="javascript: window.frameElement.commitPopup();" />