Wednesday 2 October 2013

Java Script usefull method

1. JavaScript for Date and Time Formating
//function for the getting the formatted date and time 
// current output June 26, 2012 4:04 PM

function formattedUTCDateTime(d) {

    // code for the year
    var 
m_names = new Array("January", "February", "March",
    "April", "May", "June", "July", "August", "September",
    "October", "November", "December");

//    var 
d = new Date();
    var 
curr_date = d.getUTCDate();
    var 
sup = "";


else if (
curr_date == 3 || curr_date == 23) {
        
sup = "rd";
    }

    if (
curr_date == 1 || curr_date == 21 || curr_date == 31) {
        
sup = "st";
    }
    else if (
curr_date == 2 || curr_date == 22) {
        
sup = "nd";
    }
 
    else {
        
sup = "th";
    }

    var 
curr_year = d.getUTCFullYear();
    var 
curr_month = d.getUTCMonth();


    //code for the time  
    var 
hours = d.getUTCHours()
    var 
minutes = d.getUTCMinutes()

    var 
suffix = "AM";
    if (
hours >= 12) {
        
suffix = "PM";
        
hours = hours - 12;
    }
    if (
hours == 0) {
        
hours = 12;
    }

    if (
minutes < 10)
        
minutes = "0" + minutes;

    var 
datetimevalue = (m_names[curr_month] + " " + curr_date + ", " + curr_year + "  " + hours + ":" + minutes + " " + suffix);
    return datetimevalue;

}



How to: Display a page as a Modal Dialog Box Use Webpart Visual 2010

http://mstechsharing.blogspot.in/2012/03/how-to-display-page-as-modal-dialog-box.html


Open sharepoint site, create new Custom List name is Employees
Change Title Field to EmployeeID
Create new Field FirstName, LastName
Add new some data to list
Open Visual studio 2010 | New Project | Empty Sharepoint Project | name is SanboxPopup
Choose “Deploy as a sandboxed solution”
Create Module: you can reference at here
Right click to solution | add new item | Module template | Name is Scripts
Appear Sample.txt in Scripts then rename Sample.txt to jsFunction.js
After change, result as follows
In the Module element, add a Url ="_catalogs/masterpage" RootWebOnly="TRUEList="116" attribute value
Delete Url Script/
In the File element, copy  Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" and paste to below
Open file jsFunction.js and delete content
Paste code below to content of file jsFunction.js
Expand Features treeview, rename Feature1 to ScriptsFeature
Double-click the ScriptsFeature node to open the Feature Designer.
In the Feature Designer, set the feature scope to Site.
In the Items in the Solution pane, click the Scripts module and Click the right arrow button to add the module to the feature. This moves the feature to the Items in the Feature pane (if Feature pane don’t appear)
Create new Webpart: Right click Project | Add new item | Webpart template | name is DialogDemo
Using library as follows:
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.HtmlControls;
Declare code as follows
const string jsScriptURL = "/_catalogs/masterpage/jsFunctions.js";

Copy code and paste to method CreateChildControls()
HtmlGenericControl scriptInclude = new HtmlGenericControl("script");
scriptInclude.Attributes.Add("src",SPContext.Current.Site.RootWeb.Url + jsScriptURL);
Controls.Add(scriptInclude);
HyperLink link = new HyperLink();
link.Text = "View Employees";
link.NavigateUrl = string.Concat("javascript: ShowDialog('",SPContext.Current.Site.RootWeb.Url,"/Lists/Employees/AllItems.aspx')");
this.Controls.Add(link);
In Solution Explorer, double-click the ScriptsFeature node to open the Feature Designer. Make sure that theDialogDemo Web Part is added to the feature.
Upload file  wsp and active it, you can reference how to way deploy sandbox at here, then add webpart and click to link below
Result as follows

Some Use full Methods in Coding

1. This method used to split string and add suffix like " ........."

   lnkTitle.Text = FormatText(lnkTitle.Text.Trim(), 200, " ");
   lblOwner.Text = FormatText(lblOwner.Text.Trim(), 100, "....");



    public string FormatText(string text, int len, string suffix)
        {
            if (len != -1 && text.Length > len)
            {
                int pos = text.LastIndexOfAny(" ,.".ToCharArray(), len);
                if (pos == -1)
                    pos = len;
                text = text.Substring(0, pos) + suffix;
            }
            return text;
        }

Out Put : like this launch new soon ...

2. This method used to return the Date Time in formatted .

 public string DateFormat(string date)
        {
            string datereturn = string.Empty;
            try
            {
                if (!string.IsNullOrEmpty(date))
                {
                    string format = "dd MMMM yyyy";
 
                    datereturn = Convert.ToDateTime(date).ToString(format);
                }
            }
            catch (Exception)
            {
            }
            return datereturn;
        }
 

SP.UI.ModalDailog on Client and Server side in SharePoint 2010

1.Open Alert with SharePoint ~ SP.UI.ModalDialog.showModalDialog


.ascx page

   function OpenWindow(title, url) {

        var html = document.createElement('h1');
        html.innerHTML = 'Thanks for your valuable suggestion.';
        var options = {
            html: html,
            title: title
        };
        SP.UI.ModalDialog.showModalDialog(options);
        //return false;
    }   


 <asp:ImageButton OnClick="imgbtnSend_Click" ID="imgbtnSend" runat="server" ImageUrl="~/_layouts/AlumniFBAPortal/images/proSend.jpg" />


Server side

  protected void imgbtnSend_Click(object sender, ImageClickEventArgs e)
        {

            StringBuilder sb = new StringBuilder(); 
            sb.Append("ExecuteOrDelayUntilScriptLoaded(function () {");
            sb.Append(string.Format("OpenWindow('Suggestion','{0}');", url)); 
            sb.Append("}, \"sp.js\");"); 

            Page.ClientScript.RegisterStartupScript(this.GetType(), "keyOpenMonPopUp", sb.ToString(), true); 
}   



2. open ModalPopUp Dialog on server side in SharePoint 2010

 <script type="text/javascript">
 function OpenWebPage(title, url) {
var options = {url: url,
title: title,
width: 600,
height: 400,
dialogReturnValueCallback: DialogCallback
};
SP.UI.ModalDialog.showModalDialog(options);
}

function DialogCallback(dialogResult, returnValue) {meesageId = SP.UI.Notify.addNotification(returnValue,
false);}

</script>

<table width="901px" border="0" align="left" cellpadding="0" cellspacing="0" style="background-color: White;"><tr><td colspan="5" align="left" style="height: 5px;"><input type="button" value="Open Modal Popup Dialog"id="btnAddVendors" onclientclick="return OpenModalPopupDialog('Growth Remarks Page','/_Layouts/GrowthApp/MyPage.aspx');"/><asp:Button ID="btnAddVendors" runat="server" Text="Add Vendors" CausesValidation="false"ToolTip="Click here to Add Vendor" />
</td></tr></table>

on the class.cs file 

btnAddVendors.Attributes.Add("onclick""return OpenModalPopupDialog('Edit Vendor','/_Layouts/GrowthApp/MyPage.aspx'')");




3.Open model dialog onClientclick event i SharePoint 2010



4.Refresh parent page when model dialog page is opened

When you want to refresh the parent page when user clicks on OK button on dialog page that you open from the parent page then you can use SP.UI.ModalDialog.RefreshPage

Usually we write this code in dialogReturnCallBack function.


       dialogReturnValueCallback: function(dialogResult) 
        { 
          SP.UI.ModalDialog.RefreshPage(dialogResult) 
        }