Saturday, 21 July 2012




Overview and Difference between Sandboxed solution and Farm solution For SharePoint Project Type in Visual Studio 2010

In SharePoint 2010, we can create two types of solution
1>     Sandboxed Solution
2>     Farm Solution.
So what is difference between these two solutions?
Sandboxed solutions: Run in a secure, monitored process. Sandboxed solutions can be deployed without
 requiring SharePoint administrative privileges. If you choose a sandboxed solution, you can only use
project item types that are valid in sandboxed solutions.
Farm solution:  In this solution all SharePoint project item types in are available, but deployment
needs administrative privileges and the solution must run in FULL TRUST permission LEVEL.
Which solution needs to select?
Sandbox solution is best because of following reasons.
·         It will deployed at the site collection level rather than the farm level,
      so it allows us to this lets you separate a solution so it is only offered to
      one site collection within the farm.
·         Sandboxed solutions also run in a separate process from the
      main SharePoint IIS web application process,
       and the separate process is throttled and monitored with quotas to guard the
     SharePoint site from becoming unresponsive due to a misbehaving sandboxed solution.
·         Users can deploy solutions not including requiring administrator agreement

Many useful features provide by Sandboxed solution then why we need Farm Solution?
If Code requirements to run at the Web Application of Farm Level or talk with multiple site collection,
 in that case we should go for Farm Solution


Thursday, 19 July 2012

                                    Create group programmatically



                            SPGroupCollection ogrpcoll = oweb.SiteGroups;
                            ogrpcoll.Add("Mygroupss", oweb.SiteAdministrators[0], null, "");
                            SPGroup ogrp = oweb.SiteGroups["Mygroup"];
                            SPRoleAssignment oroleassignment = new SPRoleAssignment(ogrp);
                            SPRoleDefinition roledef = oweb.RoleDefinitions["Contribute"];
                            oroleassignment.RoleDefinitionBindings.Add(roledef);
                            oweb.RoleAssignments.Add(oroleassignment);
                            Console.WriteLine("Group created");
                            Console.ReadLine();
Timerjob

public Monthlysendemail2(): base()
        {
        }
        public Monthlysendemail2(string jobNamenew, SPService servicenew, SPServer servernew, SPJobLockType targetTypenew)
         : base(jobNamenew, servicenew, servernew, targetTypenew)
        {
        }
         public Monthlysendemail2(string jobNamenew, SPWebApplication webApplication)
        : base(jobNamenew, webApplication, null, SPJobLockType.ContentDatabase)
        {
          this.Title ="MEMC_RMT_Employee2ndAlert";
        }
         public override void Execute(Guid contentDbId)
         {
             SPSecurity.RunWithElevatedPrivileges(delegate()
             {
                 SPWebApplication webApplication = this.Parent as SPWebApplication;
                 SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];
                 using (SPSite ositeval = new SPSite(contentDb.Sites[0].ID))
                 {
                      using (SPWeb oweb = ositeval.RootWeb)
                     {
                         SPList olist = oweb.Lists.TryGetList("TimeSheet Approvers");
                         if (olist != null)
                         {
                             SPQuery qry = new SPQuery();
                             qry.Query = "<Where><And><Eq><FieldRef Name='Employee_x0020_Status' /><Value Type='Choice'>active</Value></Eq><Eq><FieldRef Name='Status' /><Value Type='Text'>Not Started</Value></Eq></And></Where>";
                             SPListItemCollection oitemcoll = olist.GetItems(qry);
                             if (oitemcoll.Count > 0)
                             {
                                 foreach (SPListItem oitem in oitemcoll)
                                 {
                                     SPFieldUserValue userval = new SPFieldUserValue(oweb, oitem["Employee"].ToString());
                                     SPFieldUserValue approveremail = new SPFieldUserValue(oweb,oitem["Approver"].ToString());
                                     SPUser ouser = userval.User;
                                     SPUser appuser = approveremail.User;
                                     string appemailid = appuser.Email;
                                     if (!string.IsNullOrEmpty(appemailid))
                                     {
                                      //send email  to the approver
                                      sendemailforapprover(appemailid,oweb,ouser);
                                     }
                                     string emailid = ouser.Email;
                                     if (!string.IsNullOrEmpty(emailid))
                                     {
                                       //send email to the employee's email id
                                       sendemail(emailid, oweb);
                                     }
                                     SPGroup hrgrp = oweb.SiteGroups["HR RMT Group "];
                                     SPUserCollection hruserscoll = hrgrp.Users;
                                     if (hruserscoll.Count > 0)
                                     {
                                         foreach (SPUser ouserhr in hruserscoll)
                                         {
                                            string hremail = ouserhr.Email;
                                            sendemailforhr(hremail, oweb,ouserhr.Name.ToString());
                                         }
                                     }

                                 }
                             }
                         }
                    }
                  }
               });
            }

Feature

 public class Feature1EventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.


        //this is the jobname so that only two different timerjob for the same site can be deployed.
        const string List_JOB_NAME_Val ="MEMC_RMT_Employee2ndAlert";
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite osite = properties.Feature.Parent as SPSite;


            // make sure the job isn't already registered
            foreach (SPJobDefinition job in osite.WebApplication.JobDefinitions)
            {
               if (job.Name == List_JOB_NAME_Val)
               job.Delete();
            }
            Monthlysendemail2 listLoggerJob = new Monthlysendemail2(List_JOB_NAME_Val, osite.WebApplication);
            SPDailySchedule dailymail = new SPDailySchedule();
            dailymail.BeginHour = 11;
            dailymail.BeginMinute = 0;
            dailymail.BeginSecond = 0;
            dailymail.EndHour = 11;
            dailymail.EndMinute = 00;
            dailymail.EndSecond = 01;
            listLoggerJob.Schedule = dailymail;
            listLoggerJob.Update();


           }
           
        
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            // delete the job
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
              if (job.Name == List_JOB_NAME_Val)
              job.Delete();


            }
        }


Friday, 13 July 2012

Script for client side validation for text box



<script type="text/javascript" language="javascript">
    function Trim(TrimString) {
        var tempObj;
        if (TrimString) { tempObj = TrimString; TrimString = tempObj.value; }
        if (TrimString == "undefined" || TrimString == null) return null;
        if (TrimString.length == 0) TrimString = "";
        else {
            var Lcont = 0, TempStrArray = TrimString.split(" "), Rcont = TempStrArray.length - 1;
            while (TempStrArray[Lcont] == "") Lcont++; while (TempStrArray[Rcont] == "") Rcont--;
            if (Rcont < Lcont) TrimString = ""; else
                for (TrimString = TempStrArray[Lcont], Lcont++; Lcont <= Rcont; Lcont++) TrimString += " " + TempStrArray[Lcont];
            var re = /\'|/g;
            TrimString = TrimString.replace(re, "");
        }
        if (tempObj && TrimString != "undefined" && TrimString != null) {
            tempObj.value = TrimString; return tempObj
        }
        else return TrimString;
    }
    function fnValidation() {
        
        document.getElementById('<%= lblerror.ClientID  %>').innerHTML = "";
        document.getElementById('<%= lblerror.ClientID  %>').style.color = 'red';
        var rval = true;
        var rvalnew = true;
        var ErrorMsg = "";
        var objFocus;
        Trim(document.getElementById('<%= txtpsword.ClientID  %>'));
        Trim(document.getElementById('<%= txtreenter.ClientID  %>'));
        if (document.getElementById('<%= txtpsword.ClientID  %>').value.trim() != document.getElementById('<%= txtreenter.ClientID  %>').value.trim()) {
            ErrorMsg = "Password mismatches please enter the correct password";
            objFocus = document.getElementById('<%= txtreenter.ClientID  %>');
            rval = false;
        }
        Trim(document.getElementById('<%= txtreenter.ClientID  %>'));
        if (document.getElementById('<%= txtreenter.ClientID  %>').value == "") {
            ErrorMsg = "Fields marked with (*) cannot be left blank ";
            objFocus = document.getElementById('<%= txtreenter.ClientID  %>');
            rval = false;
        }
        var regpassword = /^.*(?=.*\d)(?=.*[@#$%^&+=]).*$/;
        Trim(document.getElementById('<%= txtpsword.ClientID  %>'));
        if (document.getElementById('<%= txtpsword.ClientID  %>').value.match(regpassword) == null) {
            ErrorMsg = "Password should contain atleast one special charcter and number";
            objFocus = document.getElementById('<%= txtpsword.ClientID  %>');
            rval = false;
        }
        Trim(document.getElementById('<%= txtpsword.ClientID  %>'));
        if (document.getElementById('<%= txtpsword.ClientID  %>').value.length < 9) {
            ErrorMsg = "Please enter the Password between 9-20";
            objFocus = document.getElementById('<%= txtpsword.ClientID  %>');
            rval = false;
        }
        Trim(document.getElementById('<%= txtpsword.ClientID  %>'));
        if (document.getElementById('<%= txtpsword.ClientID  %>').value == "") {
            ErrorMsg = "Fields marked with (*) cannot be left blank ";
            objFocus = document.getElementById('<%= txtpsword.ClientID  %>');
            rval = false;
        }
        var emailrg = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
        Trim(document.getElementById('<%= txtemail.ClientID  %>'));
        if (document.getElementById('<%= txtemail.ClientID  %>').value.match(emailrg) == null) {
            ErrorMsg = "Please enter the correct email id";
            objFocus = document.getElementById('<%= txtemail.ClientID  %>');
            rval = false;
        }
        Trim(document.getElementById('<%= txtemail.ClientID  %>'));
        if (document.getElementById('<%= txtemail.ClientID  %>').value == "") {
            ErrorMsg = "Fields marked with (*) cannot be left blank";
            objFocus = document.getElementById('<%= txtemail.ClientID  %>');
            rval = false;
        }
        Trim(document.getElementById('<%= txtphone.ClientID  %>'));
        if (document.getElementById('<%= txtphone.ClientID  %>').value.length < 10) {
            ErrorMsg = "Please enter the phone number between 10-20 characters";
            objFocus = document.getElementById('<%= txtphone.ClientID  %>');
            rval = false;
        }
        Trim(document.getElementById('<%= txtphone.ClientID  %>'));
        var regnumberphone = /[^0-9.-]+/;
        if (document.getElementById('<%= txtphone.ClientID  %>').value.match(regnumberphone) != null) {
            ErrorMsg = "Please enter number only";
            objFocus = document.getElementById('<%= txtphone.ClientID  %>');
            rval = false;
        }
        Trim(document.getElementById('<%= txtphone.ClientID  %>'));
        if (document.getElementById('<%= txtphone.ClientID  %>').value == "") {
            ErrorMsg = "Fields marked with (*) cannot be left blank ";
            objFocus = document.getElementById('<%= txtphone.ClientID  %>');
            rval = false;
        }
        Trim(document.getElementById('<%= txtexp.ClientID  %>'));
        var regnumber = /[^0-9.-]+/;
        if (document.getElementById('<%= txtexp.ClientID  %>').value.match(regnumber) != null) {
            ErrorMsg = "Please enter only number";
            objFocus = document.getElementById('<%= txtexp.ClientID  %>');
            rval= false;
        }
        Trim(document.getElementById('<%= txtexp.ClientID  %>'));
        if (document.getElementById('<%= txtexp.ClientID  %>').value == "") {
            ErrorMsg = "Fields marked with (*) cannot be left blank "
            objFocus = document.getElementById('<%= txtexp.ClientID  %>');
            rval = false;
        }
        Trim(document.getElementById('<%= txtqul.ClientID  %>'));
        if (document.getElementById('<%= txtqul.ClientID  %>').value == "") {
            ErrorMsg = "Fields marked with (*) cannot be left blank ";
            objFocus = document.getElementById('<%= txtqul.ClientID  %>');
            rval = false;
        }
        Trim(document.getElementById('<%= txtfst.ClientID  %>'));
        if (document.getElementById('<%= txtfst.ClientID  %>').value == "") {
            ErrorMsg = "Fields marked with (*) cannot be left blank ";
            objFocus = document.getElementById('<%= txtfst.ClientID  %>');
            rval = false;
        }
        if (rval == false) {
            objFocus.focus();
            document.getElementById('<%= lblerror.ClientID  %>').innerHTML = ErrorMsg;
        }
        return rval;


    }
    
</script>


 <asp:Button ID="btnsubmit" runat="server" Text="Submit" CssClass="ms-ButtonHeightWidth"
                OnClientClick="javascript:return fnValidation()" OnClick="btnsubmit_Click" />
Javascript for open application page in modelpopup



 <asp:TemplateField HeaderText="Edit" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:ImageButton ID="imgedit" runat="server" ImageUrl="~/_layouts/1033/IMAGES/edit.jpg"
                                    OnClientClick="javascript:return OpenDialogbox(this);" />
                                <asp:HiddenField ID="hdn" runat="server" Value='<%#Eval("ID")%>' />
                            </ItemTemplate>
                        </asp:TemplateField>






<script language="javascript" type="text/javascript">
    function OpenDialogbox(obj) {
        var item = obj.id;
        item = item.replace("imgedit", "hdn");
        var itemid = document.getElementById(item).value;
        var options = {
            url: "/_Layouts/TM_Mentor_TakeNotes/Edit Mentor_TakeNotes.aspx?Itemsvalue=" + itemid,
            width: 600,
            height: 300,
            showClose: true,
            title: "Edit Notes"
        };
        SP.UI.ModalDialog.showModalDialog(options);
        return false;
    }  
</script>
BCS

http://www.dotnetcurry.com/ShowArticle.aspx?ID=794