SharePoint List CRUD Operation using JavaScript

In order to my last post “SharePoint List CRUD Operation using PowerShell Management”. I am going to help you in learning how to do basic SharePoint List operations (CRUD – Create, Read, Update and Delete) using JavaScript.

Create SharePoint List item

function createSPListItem() {
        var listName = "DemoList"; //Give your list name
        var clientContext = new SP.ClientContext.get_current(); 
        var oWeb = clientContext.get_web();
        var oList = oWeb.get_lists().getByTitle(listName);

        var itemCreateInfo = new SP.ListItemCreationInformation();
        this.oListItem = oList.addItem(itemCreateInfo);
        this.oListItem.set_item("Title", "Test-Item-2");
        this.oListItem.set_item("DemoLocation", "India");
        this.oListItem.set_item("ProjectName", "CSR-RL-09");
        this.oListItem.update();

        clientContext.load(this.oListItem);
        clientContext.executeQueryAsync(
            Function.createDelegate(this, successHandler),
            Function.createDelegate(this, errorHandler)
        );

        function successHandler() {
            alert('Item ' + this.oListItem.get_id() + 'created sucessfully!');
        }

        function errorHandler() {
            alert("Request failed: " + arguments[1].get_message());
        }
    }

Read SharePoint List item

    function readSPList() {
        var listName = "DemoList"; //Give your list name
        var clientContext = new SP.ClientContext.get_current();
        var oWebsite = clientContext.get_web();
        var oList = oWebsite.get_lists().getByTitle(listName);
        var camlQuery = new SP.CamlQuery();
        // set_viewXml function to define a CAML query and return items that meet specific criteria.
        // you can write caml query into below line
        camlQuery.set_viewXml('<View><RowLimit>100</RowLimit></View>');
        // The getItems(query) function enables you to define a Collaborative Application Markup Language (CAML) query that specifies which items to return
        this.collListItem = oList.getItems(camlQuery);

        clientContext.load(this.collListItem, "Include(Title, DemoLocation, ProjectName)");
        clientContext.executeQueryAsync(
            Function.createDelegate(this, successHandler),
            Function.createDelegate(this, errorHandler)
        );

        function successHandler() {
            var listItemInfo;
            var listItemEnumerator;

            listItemEnumerator = this.collListItem.getEnumerator();

            listItemInfo = "";
            while (listItemEnumerator.moveNext()) {
                var oListItem = listItemEnumerator.get_current();
                listItemInfo += "Title: " + oListItem.get_item('Title') + "<br/>" +
                "Location: " + oListItem.get_item('DemoLocation') + "<br/>" +
                "Project Name: " + oListItem.get_item('ProjectName') + "<br/>";
            }

            alert(listItemInfo);
        }

        function errorHandler() {
            resultpanel.innerHTML = "Request failed: " + arguments[1].get_message();
        }
    }
	

Update SharePoint List item

    function updateSPListItem() {
        var listName = "DemoList"; //Give your list name
        var clientContext = new SP.ClientContext.get_current();
        var oWeb = clientContext.get_web();
        var oList = oWeb.get_lists().getByTitle(listName);

        this.oListItem = oList.getItemById(1);
        this.oListItem.set_item("Title", "Test-Item-03");
        this.oListItem.set_item("DemoLocation", "SIG");
        this.oListItem.set_item("ProjectName", "NDR-PO-09");
        this.oListItem.update();

        clientContext.load(this.oListItem);
        clientContext.executeQueryAsync(
            Function.createDelegate(this, successHandler),
            Function.createDelegate(this, errorHandler)
        );

        function successHandler() {
            alert('Item ' + this.oListItem.get_id() + 'updated sucessfully!');
        }

        function errorHandler() {
            alert("Request failed: " + arguments[1].get_message());
        }
    }
	

Delete SharePoint List item

    function deleteSPListItem() {
        var listName = "DemoList"; //Give your list name
        var clientContext = new SP.ClientContext.get_current();
        var oWeb = clientContext.get_web();
        var oList = oWeb.get_lists().getByTitle(listName);

        this.oListItem = oList.getItemById(1);
        this.oListItem.deleteObject();

        clientContext.executeQueryAsync(
            Function.createDelegate(this, successHandler),
            Function.createDelegate(this, errorHandler)
        );

        function successHandler() {
            alert('Item ' + this.oListItem.get_id() + 'deleted sucessfully!');
        }

        function errorHandler() {
            alert("Request failed: " + arguments[1].get_message());
        }
    }
	

SharePoint List CRUD Operation using PowerShell Management

This post is going to help you in learning how to do basic SharePoint List operations (CRUD – Create, Read, Update and Delete) using SharePoint Management Shell or Windows PowerShell.

Lets open SharePoint Management Shell or open Windows PowerShell.When we are working on SharePoint Server there are two possibilities exists: either select the SharePoint Management Shell or Open Windows PowerShell.If We are using SharePoint Management Shell then SharePoint snap-in will already be installed.If you are using Standard PowerShell console, we can install the snap-in by entering the following command:

Add-PSSnapIn Microsoft.SharePoint.PowerShell

we can check the list of installed snap-in by using this command

Get-PSSnapIn

So, here is my list like below screenshot.

CRUDPowershell1

1) Create Item into SharePoint List

$webURL="http://optimumview"
$lstName="DemoList"
$oWeb=Get-SPWeb -Identity $webURL
$oList=$oWeb.Lists[$lstName]
$oListItem = $oList.items.add()
$oListItem["Title"]="Test-Item-0"
$oListItem["DemoLocation"]="US"
$oListItem["ProjectName"]="POW-JO-09"
$oListItem.Update()

2) Read single list item

$webURL="http://optimumview"
$lstName="DemoList"
$oWeb=Get-SPWeb -Identity $webURL
$oList=$oWeb.Lists[$lstName]
#To read single item uncomment below code
#$oListItem = $oList.GetItemById("1")
#Read multiple list item
$oListItem = $oList.Items | where {$_['ProjectName'] -like "IND1*"}
if($oListItem -ne $null)
{
$oListItem | ForEach-Object {
Write-Host $_['ID'] -foregroundcolor green
Write-Host $_['Title'] -foregroundcolor green
Write-Host $_['DemoLocation'] -foregroundcolor green
Write-Host $_['ProjectName'] -foregroundcolor green
}
}
else
{
Write-Host "No Items are found !!" -foregroundcolor red
}

3) Update SharePoint List item

#Update multiple list item

$webURL="http://optimumview"
$lstName="DemoList"
$oWeb=Get-SPWeb -Identity $webURL
$oList=$oWeb.Lists[$lstName]
$oListItem = $oList.Items | where {$_['ProjectName'] -like "IND*"}
if($oListItem -ne $null)
{
$oListItem | ForEach-Object {
$_['Title']="Test1" + $_['ID']
Write-Host "Item id:" $_['ID'] " has been updated!" -foregroundcolor green
$_.Update()
}
}
else
{
Write-Host "No Items are found !!" -foregroundcolor red
}

4) Deleting SharePoint List item

#Deleting multiple list item
$webURL="http://optimumview"
$lstName="DemoList"
$oWeb=Get-SPWeb -Identity $webURL
$oList=$oWeb.Lists[$lstName]
$oListItem = $oList.Items | where {$_['ProjectName'] -like "GLT*"}
if($oListItem -ne $null)
{
$oListItem | ForEach-Object {
$_.Delete()
Write-Host "Item id:" $_['ID'] " has been Deleted!" -foregroundcolor green
}
}
else
{
Write-Host "No Items are found !!" -foregroundcolor red
}

Hiding the Quick Launch in SharePoint 2013

  • Open and Edit SharePoint Page.
  • Add Script Editor or Content Editor Web part into anywhere in your page, and add the below code
<style>
//removes the Quick Launch
.ms-core-navigation { DISPLAY: none }
//Move all content of the page to left
#contentBox { margin-left: 0px }
</style>
  • Save the Page. The Quick Launch section should not be visible now.

How to show attachment with List item in separate column.

Today I got the client requirement to display item attachment in List view.I am going to present the steps to display item attachment into SharePoint 2010 List view.

1) Open SharePoint List view in SharePoint Designer.

2) Open SharePoint list > Under View > Right click on Target View File in my case it is “All Items” and select “Edit File in Advanced Mode ” .

3) Select the view and create new column titled Attachment.

4) Select TD tag of New column.

5) And Replace the selected TD tag to below code.

<strong><td</strong>id="ItemAttchment"class="ms-vb"<strong>>/strong>

<strong><xsl:element</strong>name="SharePoint:AttachmentsField"<strong>&gt;</strong>

<strong><xsl:attribute</strong>name="runat"<strong>&gt;</strong>server<strong>&lt;/xsl:attribute&gt;</strong>

<strong><xsl:attribute</strong>name="FieldName"<strong>&gt;</strong>Attachments<strong>&lt;/xsl:attribute&gt;</strong>

<strong><xsl:attribute name="ControlMode"<strong>&gt;</strong>Display<strong>&lt;/xsl:attribute&gt;</strong>

<strong><xsl:attribute name="Visible"<strong>&gt;</strong>true<strong>&lt;/xsl:attribute></strong>

<strong><xsl:attribute</strong>name="ItemId"<strong>></strong>

<strong><xsl:value-of select="$thisNode/@ID"/></strong>

<strong></xsl:attribute></strong>

<strong></xsl:element></strong>

<strong></td></strong>

6)  The final output will like below :

FinalImage

Customize Sharepoint Summary Link Webpart

Step 1.

  • Open SharePoint Using System Account >
  • Navigate to Site Actions > Edit Page > Add a Summary Link Web Part

Step 2.

  • Open SharePoint Using System Account >
  • Navigate to Site Actions > Edit Page > Add a Content Editor Web Part
  • In the Web Part Click on The option Click here to Add New Content, And on the ribbon HTML > Edit HTML Source Then Paste the below CSS Code.
<style>
.groupheader
{
    PADDING-BOTTOM:0.5em;
	BACKGROUND-COLOR:#0072c6;
    PADDING-LEFT:0.75em;
    PADDING-RIGHT:0.5em;
    FONT-FAMILY:“Segoe UI”,Tahoma,Geneva,Verdana,sans-serif;
    MARGIN-BOTTOM:1px;
    COLOR:white;
    FONT-SIZE:140%;
    MARGIN-RIGHT:0.5em;
    PADDING-TOP: 0.5em;
}
.groupmarker:hover.groupheader
 {
	BACKGROUND-COLOR: #0597ff;
	CURSOR: pointer;
}
.dfwp-list
 {
	BACKGROUND-COLOR:#0072c6;
	MARGIN-BOTTOM:0.5em;
	MARGIN-RIGHT:0.75em
}
.dfwp-list.item:hover
 {
	BACKGROUND-COLOR:#0597ff
}
.dfwp-list.link-itemA
{
    COLOR:white;
    MARGIN-LEFT:2em;
}
<style>

Hiding the Quick Launch in SharePoint 2010

  • Open SharePoint portal
  • Navigate to Site Action > Edit page
  • Add a new Content Editor webpart.
  • In the Web Part Click on The option Click here to Add New Content, And on the ribbon HTML > Edit HTML Source Then Paste the below CSS Code.
<style>
#s4-leftpanel
{
display:none
}
.s4-ca{
margin-left:0px
}
</style>

  • Save the Page. The Quick Launch section should not be visible now.
  • Edit the Just added Content Editor web Part and Under Layout Section Tick the Hidden Check box and Click OK, Which will hide the Webpart from Normal View.