Friday 15 November 2013

How to create Sharepoint List using Client Object Model (JavaScript) ?

To Create a SP List follow the below steps :

Step 1 : Add HTML TextBox and Button fields to HTML file as below.

          <h2>List Creation</h2>
          <table >
            <tr>
               <td>
                    List Name :
               </td>
               <td>
                   <input id="tbList" type="text" />
               </td>
            </tr>
            <tr>
               <td >
                   <input type="button" id="btnCreateList" value="Create List" onclick="CreateList()" />
               </td>
            </tr>
         </table>

Step 2 : Add below Script to create a SPList when you fill Textbox and click 'OK' button.

    <script type="text/javascript">
    var siteUrl = "/";  //give your site url. Ex: sitecollectionTitle/siteTitle
    function CreateList() {
        var listName = document.getElementById("tbList");
        var clientContext = new SP.ClientContext(siteUrl);
        var oWebsite = clientContext.get_web();
        var listCreationInfo = new SP.ListCreationInformation();
        listCreationInfo.set_title(listName.value);
        listCreationInfo.set_templateType(SP.ListTemplateType.genericList);
        var oList = oWebsite.get_lists().add(listCreationInfo);
        clientContext.load(oList);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }
    function onQuerySucceeded() {
        alert('Request successfully completed');
    }
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
   </script>


Step 3 :  Execute the above code.Enter the list name in the textbox and click button. Now the list gets created.

Enjoy Coding....


Friday 1 November 2013

Send Email using C#

Add the "System.Net.Mail" namespace at the top of the class and follow the below code to send email using SMTP server.

       public void SendMail()
      {
           try
           {
               MailMessage mail = new MailMessage();
               mail.From = new MailAddress("YourEmailID@gmail.com");
               mail.Bcc.Add("bccEmail@gmail.com");
               mail.CC.Add("sampleEmail@ymail.com");
               mail.To.Add("FriendEmailID@yahoo.com");
               mail.Subject = "Test Mail";
               mail.Body = "Hi...!!! This is a test mail.";
               mail.IsBodyHtml = true;
               SmtpClient smtpClient = new SmtpClient();
               smtpClient.Host = "smtp.gmail.com";
               smtpClient.Port = 587;
               smtpClient.UseDefaultCredentials = false;
               smtpClient.Credentials = new System.Net.NetworkCredential("YourEmailID@gmail.com""YourPassword");
               smtpClient.EnableSsl = true;
               ServicePointManager.ServerCertificateValidationCallback = delegate
               {
                   return true;
               };
               smtpClient.Send(mail);
           }
           catch (Exception ex)
           {
                 //Your Exception code
           }
       }

Total Pageviews