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
           }
       }

Wednesday, 23 October 2013

Difference between List Definition, List Template and List Instance in Sharepoint

List Definition:
A list definition defines a schema for a SharePoint list. It contains information on what views are being used, which columns and content types are being used, and other metadata information.
 
List Template:
A list template can either be created by end users through the SharePoint user interface using an existing list as a pattern or using an existing list instance. If based on a user-created list it is called a custom list template. A custom list template includes everything that defines the list, including list columns and site columns used by the list, content types used by the list, views defined for the list, and so on.
 
Tips
A list template may sound like a list definition but they are effectively the same thing, a pattern for a SharePoint list. They differ mainly in how they are created:
- A list templates are created in SharePoint or SharePoint designer.
- A list definitions in Visual Studio.

List Instance:
A list instance is an instance of a specific SharePoint list definition or list template. All of its data is stored in the relevant content database. Typically a list in SharePoint used by end users to enter and view data and it is based on either a list template or list definition.

Sunday, 20 October 2013

Retrieving table data from SQL Server using C#

This is a simple task which can be accomplished with below code. Below code is useful to retrieve table data from SQL Server using C#.net .

    public List<Contact> LoadContacts()
    {
        List<Contact> ContactDetails= new List<Contact>();
       string selectQuery = "SELECT * FROM Contact";
        SqlConnection connection = new SqlConnection("Data Source=(local); Initial Catalog = DataBaseName; Integrated Security=SSPI");
        SqlCommand cmd = new SqlCommand(selectQuery , connection );
        try
        {
            connection.Open();
            SqlDataReader dataReader = command.ExecuteReader();
            while (dataReader.Read())
            {
                Contact contact = new Contact();
                contact.ID = Convert.ToInt32(dataReader["ID"]);
                contact.Name = dataReader["Name"].ToString();
                contact.EmailID = dataReader["EmailID"].ToString();
                contact.Mobile = dataReader["Mobile"].ToString();
                contact.LandLine = dataReader["LandLine"].ToString();
                contact.WebSite = dataReader["WebSite"].ToString();
                contact.Address = dataReader["Address"].ToString();
                ContactDetails.Add(contact);
            }
            dataReader.Close();
        }
         catch(Exception e)
        { }
        finally
        {
           connection .Close();
         }
         return ContactDetails;
    }


Connection string's options :
  •   Integrated Security=SSPI; – This means we want to connect using Windows authentication.
  •  Initial Catalog=DataBaseName; – This means the database we want to first connect to is named “DataBaseName”.
  •  Data Source=(local); – This means that we want to connect to the SQL Server instance located on the local machine.

Delete data from SQL Server using C#.net

This is simple task which can be accomplished with below code. Below code is useful to Delete data (contact) from SQL Server database using C#.net .

    public void DeleteContact_OnClick(object sender, EventArgs e)
    {
       string myDeleteCommand = @"DELETE Contact where ID=@ID";

        SqlConnection connection = new SqlConnection("Data Source=(local); Initial Catalog = DataBaseName; Integrated Security=SSPI");
        SqlCommand cmd = new SqlCommand(myDeleteCommand , connection );
        cmd.Parameters.AddWithValue("@ID", Convert.ToInt32( tbID.Text));
        try
        {
            connection .Open();
            cmd.ExecuteNonQuery();
         }
         catch(Exception e)
        { }
        finally
        {
           connection .Close();
         }
    }


Connection string's options :
  •   Integrated Security=SSPI; – This means we want to connect using Windows authentication.
  •  Initial Catalog=DataBaseName; – This means the database we want to first connect to is named “DataBaseName”.
  •  Data Source=(local); – This means that we want to connect to the SQL Server instance located on the local machine.

Total Pageviews