Wednesday 4 December 2013

Insert,update,delete in sharepoint list using Javascript


<input type="button" value="Submit"  onclick="createListItem();" style="width: 89px" />
<input type="button" value="Update"  onclick="updateListItem();" style="width: 89px" />
<input type="button" value="Delete"  onclick="deleteListItem();" style="width: 89px" />

<script type="text/javascript">
    var siteUrl = '/sites/TeamSite1';
    function createListItem() {
        var clientContext = new SP.ClientContext(siteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle('Employee');
        var itemCreateInfo = new SP.ListItemCreationInformation();
        this.oListItem = oList.addItem(itemCreateInfo);
        oListItem.set_item('Title', '1');
        oListItem.set_item('EmpName', 'Mohamed sithik');
        oListItem.set_item('Age', '27');
        oListItem.set_item('Address', 'Chennai');
        oListItem.update();
        clientContext.load(oListItem);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }
    function onQuerySucceeded() {
        alert('Item created: ' + oListItem.get_id());
    }
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }

</script>
<script>
    var siteUrl = '/sites/TeamSite1';
    function updateListItem() {
        var clientContext = new SP.ClientContext(siteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle('Employee');
        this.oListItem = oList.getItemById(333);
        oListItem.set_item('Title', '4');
        oListItem.set_item('EmpName', 'sithik');
        oListItem.set_item('Age', '26');
        oListItem.set_item('Address', 'Bangalore');
        oListItem.update();
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }
    function onQuerySucceeded() {
        alert('Item updated!');
    }
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
</script>
<script>
     var siteUrl = '/sites/TeamSite1';

     function deleteListItem() {
         this.itemId = 333;
         var clientContext = new SP.ClientContext(siteUrl);
         var oList = clientContext.get_web().get_lists().getByTitle('Employee');
         this.oListItem = oList.getItemById(itemId);
         oListItem.deleteObject();
         clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
     }

     function onQuerySucceeded() {
         alert('Item deleted: ' + itemId);
     }

     function onQueryFailed(sender, args) {
         alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
     }


    </script>
Continue Reading...

Delete all SharePoint List Item using Powershell in SharePoint

Sytax:-

$SITEURL = "http://serverurl"

$site = new-object Microsoft.SharePoint.SPSite ( $SITEURL )
$web = $site.OpenWeb()
"Web is : " + $web.Title

$oList = $web.Lists["LIST NAME"];

"List is :" + $oList.Title + " with item count " + $oList.ItemCount

$collListItems = $oList.Items;
$count = $collListItems.Count - 1

for($intIndex = $count; $intIndex -gt -1; $intIndex--)
{
        "Deleting record: " + $intIndex
        $collListItems.Delete($intIndex);
}

Continue Reading...

Tuesday 3 December 2013

join two sharepoint list using c#


1.Create a custom list name as "List1" with below column

2.Create a custom list name as "List2" with below column

3.paste the below code inside the webpart

public void GetEmployee()
        {
            SPWeb objweb = SPContext.Current.Web;
            SPList olist1 = objweb.Lists["list1"];
            SPListItemCollection collListItems = olist1.Items;
            SPQuery oQuery1 = new SPQuery();
            DataTable table1 = collListItems.GetDataTable();
            SPList olist2 = objweb.Lists["list2"];
            SPListItemCollection collListItems2 = olist2.Items;
            DataTable table2 = collListItems2.GetDataTable();
            DataTable dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Age", typeof(string));
            dt.Columns.Add("Salary", typeof(string));
            dt.Columns.Add("Sex", typeof(string));
            dt.Columns.Add("Address", typeof(string));
            var Vout = from tbl1 in table1.AsEnumerable()
                       join tbl2 in table2.AsEnumerable() on tbl1["Name"] equals tbl2["Name"]
                       select new
                       {
                           Sex = tbl2["Sex"],
                           Address = tbl2["Address"],
                           Name = tbl1["Name"],
                           Age = tbl1["Age"],
                           Salary = tbl1["Salary"]
                       };
            foreach (var item in Vout)
            {
                DataRow dr = dt.NewRow();
                dr["Name"] = item.Name;
                dr["Age"] = item.Age;
                dr["Salary"] = item.Salary;
                dr["Sex"] = item.Sex;
                dr["Address"] = item.Address;
                dt.Rows.Add(dr);
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
4.output



Continue Reading...

Timer Job Example in SharePoint 2013


1.open visual studio 2012
2.Add SharePoint2013-Empty Project named it "TimerJobExample"
3.Enter your site collection and choose Deploy as a Farm solution
4.Add class file in your solution named it "Employee"
5.paste the below code inside the Employee class

 public class Employee : SPJobDefinition
    {
        public Employee()
            : base()
        {
            Title = "Sithik Job";

        }

        public const string JobName = "Sithik Job";
        public Employee(SPWebApplication webApp) :
            base(JobName, webApp, null, SPJobLockType.Job) { }
        public override void Execute(Guid targetInstanceId)
        {
            AddNewItem();

        }
  public void AddNewItem()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList listEmpInsert = web.Lists["Employee"];
                        web.AllowUnsafeUpdates = true;
                        SPListItem EmpInsert = listEmpInsert.Items.Add();
                        EmpInsert["EmpName"] = "Mohamed";
                        EmpInsert["Age"] = "28";
                        EmpInsert["Address"] = "Chennnai";
                        EmpInsert.Update();
                        web.AllowUnsafeUpdates = false;
                    }
                }
            });
        }  

    }
6.Go to Features right click add one new features and rename it "Employee"
7.Right click the Employee Features  add one "Event receiver" paste the below code inside the receiver

 private void DeleteJob(Microsoft.SharePoint.Administration.SPJobDefinitionCollection jobs)
        {
            foreach (SPJobDefinition job in jobs)
            {
                if (job.Name.Equals(TimerJobExample.Employee.JobName,
                StringComparison.OrdinalIgnoreCase))
                {
                    job.Delete();
                }
            }
        }

   public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            DeleteJob(webApp.JobDefinitions);


            TimerJobExample.Employee simpleJob = new TimerJobExample.Employee(webApp);

            SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            schedule.Interval = 1;

            simpleJob.Schedule = schedule;
            simpleJob.Update();
        }

      public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            DeleteJob(webApp.JobDefinitions);
        }

8.click the "Employee Features" Change the scope to "WebApplication"
9.click the "properties windows" "Always Force to Install" to "True"
10.Deploy the project
11.Go "central administration" click "Monitoring"
12.under "Timer Jobs" click "Review Job definitions"
13.you will see your Timer job named "Sithik Job"



Continue Reading...

Create sharepoint list custom form via visual studio 2012

1.open visual studio 2012
2.Add Sharepoint 2013 Empty project Named it "Employee"
3.Create a custom list name as "Employee" and add the below columns

4.Right click the project add the layouts folder inside that add one Application page named it "EmpCustomForm"
5.Paste the below code inside the Content ID "Main"


      <p style="font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif">&nbsp; Employee Custom Form</p>
    <table>
                <tr>
                    <td class="auto-style1">
                        <asp:Label ID="LblTitle" runat="server" Text="Title: "></asp:Label>
                    </td>
                   
                    <td>
                         <asp:TextBox ID="Txttile" runat="server" Height="46px"  Width="470px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                         <asp:Label ID="LblName" runat="server" Text="Name : "></asp:Label>
                    </td>
                    
                    <td>
                         <asp:TextBox ID="TxtName" runat="server" Height="46px"  Width="470px"></asp:TextBox>
                    </td>
                </tr>
                 <tr>
                    <td>
                         <asp:Label ID="LblAge" runat="server" Text=" Age : "></asp:Label>
                    </td>

                    <td>
                           <asp:TextBox ID="TxtAge" runat="server" Height="46px"  Width="470px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                         <asp:Label ID="LblEmail" runat="server" Text=" Email : "></asp:Label>
                    </td>
                    
                    <td>
                        <asp:TextBox ID="TxtEmail" runat="server" Height="46px"   Width="470px"></asp:TextBox>
                    </td>
                </tr>
                       </table>
        <table>
            <tr>
                <td>
                    <asp:Button ID="BtnSave" runat="server" Text="Save" OnClick="BtnSave_Click" Width="94px" />
                </td>
                <td>
                    <asp:Button ID="BtnClear" runat="server" Text="Clear" OnClick="BtnClear_Click" Width="102px" />
                </td>
            </tr>
        </table>
6.Paste the below code inside the cs page

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadData();
            }
        }
        private void LoadData()
        {
            var list = SPContext.Current.Web.Lists[new Guid(Request.QueryString["list"])];
            var itemId = 0;
          
            if (string.IsNullOrEmpty(Request.QueryString["id"]) == false)
            {
                int.TryParse(Request.QueryString["id"], out itemId);
            }
            if (itemId != 0)
            {
                var listItem = list.GetItemById(itemId);
                Txttile.Text = (listItem["Title"] == null) ? string.Empty : listItem["Title"].ToString();
                TxtName.Text = (listItem["EmpName"] == null) ? string.Empty : listItem["EmpName"].ToString();
                TxtAge.Text = (listItem["Age"] == null) ? string.Empty : listItem["Age"].ToString();
                TxtEmail.Text = (listItem["Email"] == null) ? string.Empty : listItem["Email"].ToString();
                SPSite sites = new SPSite(SPContext.Current.Site.ID);
                SPWeb webs = sites.OpenWeb();
                SPList lists = webs.Lists["Employee"];
                SPListItem item = listItem;
            }

        }
        protected void BtnSave_Click(object sender, EventArgs e)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                var list = SPContext.Current.Web.Lists[new Guid(Request.QueryString["list"])];
                string ItemsID = string.Empty;
                var itemId = 0;
                if (string.IsNullOrEmpty(Request.QueryString["id"]) == false)
                {
                    int.TryParse(Request.QueryString["id"], out itemId);
                }
                SPListItem listItem = null;
                if (itemId == 0)
                {
                    listItem = list.AddItem();
                }
                else
                {
                    listItem = list.GetItemById(itemId);
                }
                ItemsID = Convert.ToString(itemId);
                listItem["Title"] = Txttile.Text;
                listItem["EmpName"] = TxtName.Text;
                listItem["Age"] = TxtAge.Text;
                listItem["Email"] = TxtEmail.Text;
                listItem.Update();
            });
            Txttile.Text = string.Empty;
            TxtAge.Text = string.Empty;
            TxtName.Text = string.Empty;
            TxtEmail.Text = string.Empty;
        }
        protected void BtnClear_Click(object sender, EventArgs e)
        {
            Txttile.Text = string.Empty;
            TxtAge.Text = string.Empty;
            TxtName.Text = string.Empty;
            TxtEmail.Text = string.Empty;

        }
7.Go to the Employee List click the Schema.xml add the below lines

        <XmlDocuments>
          <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url">
            <FormUrls xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url">
              <Display>/sites/Test1/_layouts/15/Employee/EmpCustomForm.aspx</Display>
              <Edit>/sites/Test1/_layouts/15/Employee/EmpCustomForm.aspx</Edit>
              <New>/sites/Test1/_layouts/15/Employee/EmpCustomForm.aspx</New>
            </FormUrls>
          </XmlDocument>
        </XmlDocuments>
8. ouput



Continue Reading...

Followers

Follow The Author