Search This Blog

Friday, 27 September 2013

Object Disposal best practice

If we are not performing any operations on the SPSite object, we can write as in the following code example.

void CombiningCallsBestPractice()
{
    using (SPSite siteCollection = new SPSite(SPContext.Current.Web.Url))
    using (SPWeb web = siteCollection.OpenWeb())
        {
        //Perform operations on site.
        } // SPWeb object web.Dispose() automatically called; SPSite object
          // siteCollection.Dispose() automatically called.
}

Thursday, 26 September 2013

radio button validation using jquery in sharepoint Designer form

<script  type="text/javascript">
   $(function() {
    var str = $('input[name*=ff200]').get(1).id;
    document.getElementById(str).setAttribute('checked','checked');
  })
</script>
Here we have a Radio button with Yes and No Options.

The XSLT of Radio button in SharePoint is

<SharePoint:FormField runat="server" id="ff200{$Pos}" ControlMode="Edit" FieldName="FeedbackCompleted" __designer:bind="{ddwrt:DataBind('u',concat('ff200',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@FeedbackCompleted')}"/>



$('input[name*=ff200]') - It searches the input element whose name contains ff200 which is the in ID of the SharePoint FormField.

var str = $('input[name*=ff200]').get(1).id; line finds the ID of the Element, As radio button has two or more options but in SharePoint it is FormField. So you want to find id of First Radio Button use get(0), Second Radio button use get(1) etc. 

Tuesday, 24 September 2013

How to display text box value of custom form in Indian rupees format


Use the following code to get the text value to be assigned to the text box/label of the currency field in VS form. 

double aNumber = 2073635.00; 
System.Globalization.CultureInfo info = System.Globalization.CultureInfo.GetCultureInfo("hi-IN"); 
string str = aNumber.ToString("C2",info); 

And now str is: "(Rupee sign) 20,73,635.00" 

Wednesday, 18 September 2013

Sharepoint Designer Forms-Conditional Display of fields


<xsl:choose>
    <xsl:when test="@Status != 'Closed'">
     <SharePoint:FormField runat="server" id="ff1{$Pos}" ControlMode="Edit" FieldName="Title" __designer:bind="{ddwrt:DataBind('u',concat('ff1',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Title')}"/>
     <SharePoint:FieldDescription runat="server" id="ff1description{$Pos}" FieldName="Title" ControlMode="Edit"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:value-of select="@Title"></xsl:value-of>
    </xsl:otherwise>
</xsl:choose>

Monday, 9 September 2013

Sharepoint ClientPeoplePicker Validation

In user control page
<SharePoint:ClientPeoplePicker
            id="ppApprover"
            runat="server" Rows="1" AllowMultipleEntities="false" CssClass="ms-long"/>
                 <asp:CustomValidator ID="cstmVald2" runat="server" ControlToValidate="ppApprover" Display="Dynamic" ValidationGroup="vgCreateSE"
              ValidateEmptyText="true" SetFocusOnError="true" ErrorMessage="Please enter SDU Head(GM & above)" CssClass="ms-error" ClientValidationFunction="peoplvalidator"></asp:CustomValidator>


function peoplvalidator(sender, args) {
          args.IsValid = false;
          var user = $("span.ms-entity-resolved").attr("title");//Returns the Login Name
        
          if (user != undefined) {
              args.IsValid = true;
          }
       
      }

Ajax calls to services not working (Cache Problem)


var thiSite = _spPageContextInfo.webAbsoluteUrl;
var servicePath= "/_vti_bin/WCF/LeaveService.svc/Data";
            var MyUrl = thiSite + servicePath; 
$.ajax({
                type: "GET",
                url: MyUrl,
                cache:false,//Ensure that calls are not depend on cache
                contentType: "application/json; charset=utf-8",
                dataType: 'json',
                processdata: true,
                success: function (msg) {
                    alert("Sucess");
                    var Array = new Array();
                    //Gets the Data                    $.each(msg, function (i, item) {
                        Array[i] = item;
                    });                                                      },
                error: function (msg) { alert("Eror"); }
            });

Get Current site name from javascript


var site=_spPageContextInfo.webAbsoluteUrl;