Search This Blog

Monday, July 27, 2015

Create List View using SharePoint REST API


function createView(ListName, query )
{

var bodyContent = "{ '__metadata': { 'type': 'SP.View' }, 'Title': 'View Name', 'PersonalView': true, 'ViewQuery':'" + query + "' }";

var viewUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/lists/getbytitle('"+ListName+"')/views"

 var executor;  executor = new SP.RequestExecutor(_spPageContextInfo.webAbsoluteUrl);  executor.executeAsync({ url: viewUrl,

method: "POST",

body: bodyContent,

headers: { 

"accept": "application/json;odata=verbose",

"content-type": "application/json;odata=verbose"

},

success: function (results) { alert("View Created.") },

error: function (error) { alert("Error in view creation.") }

});

}

Call this function you have to pass two parameter ListName and query.
ListName : This is name of list in which you want to create view.

Query : You have to pass CAML Query base on which filter is set and your view is created. Like

 '<Where><Eq><FieldRef Name=\"'Title'\" /><Value Type=\"Text\">' + TitleVar +'</Value></Eq</Where>';




You can customize your view but adding more attribute in body.

This code create a personal view because i set 'PersonalView' : true. If you want to create public view than set it to false. 

var bodyContent = "{ '__metadata': { 'type': 'SP.View' }, 'Title': 'View Name', 'PersonalView': true, 'ViewQuery':'" + query + "' }";


For more detail check https://msdn.microsoft.com/en-us/library/office/dn531433.aspx#bk_View

Note : 

1. Please make sure that you have loaded SP.RequestExecutor.js script file. To load script file

var scriptbase = _spPageContextInfo.webAbsoluteUrl+ "/_layouts/15/";

$.getScript(scriptbase + "SP.RequestExecutor.js", yourFunctionName);


2. You have proper permission to create views.

SharePoint CRUD Operations on ListItem PropertyBag

# Initialize the SharePoint PowerShell environment
Add-PSSnapIn "Microsoft.SharePoint.Powershell" -ErrorAction:SilentlyContinue



# Function to perform add , delete , view and update operation on list item properties
# Parameters :
# -- ItemBagKey : Name of property of list item
# -- Operation : Type of operation  "ADD" , "UPDATE" , "DELETE" , "VIEW"
# -- NewValue : Value for the item property 
# -- Item : List item to operate upon 


Function UpdateKey{
      param([string]$ItemBagKey,[string]$Operation,[string]$NewValue,[Microsoft.SharePoint.SPListItem]$item)
      if($Operation -eq "ADD"# If we are adding property to item.
      {
            if($item.Properties.ContainsKey($ItemBagKey))
            {
                  Write-Host "Key already found in item bag." -ForegroundColor Yellow;
                  $item.Properties[$ItemBagKey] = $NewValue;
                  $item.SystemUpdate($false);
                  Write-Host "Key update with new value : "  $NewValue - ForegroundColor Green;
            }
            else
            {
                  Write-Host "Key not found in item bag." -ForegroundColor Yellow;
                  $item.Properties.Add($ItemBagKey,$NewValue);
                  $item.SystemUpdate($false);
                  Write-Host "Key added with value : "  $NewValue -ForegroundColor Green;
            }
      }
      elseif($Operation -eq "DELETE"# If we are deleting property from item.
      {
            if($item.Properties.ContainsKey($ItemBagKey))
            {
                  $item.Properties.Remove($ItemBagKey);
                  $item.SystemUpdate($false);
                  Write-Host "Key removed from item bag : "  $ItemBagKey -ForegroundColor Green;
            }
            else
            {
                  Write-Host "Key not found in item bag to remove." -ForegroundColor Red;
            }
      }
      elseif($Operation -eq "UPDATE"# If we are updating property of item.
      {
            if($item.Properties.ContainsKey($ItemBagKey))
            {
                  $item.Properties[$ItemBagKey] = $NewValue;
                  $item.SystemUpdate($false);
                  Write-Host "Key updated with new value : "  $NewValue -ForegroundColor Green;
            }
            else
            {
                  Write-Host "Key not found in item bag." -ForegroundColor Red;
            }
      }
      elseif($Operation -eq "VIEW"# If we want to view value item property.
      {
            if($item.Properties.ContainsKey($ItemBagKey))
            {
                  Write-Host "Property value : "  $item.Properties[$ItemBagKey] -ForegroundColor Yellow;
            }
            else
            {
                  Write-Host "Key not found in item bag." -ForegroundColor Red;
            }
      }
}

try
{
     
      #Put your site URL in placeholder <siteurl> 
      $web = Get-SPWeb <siteurl>     

      #Put list name in placeholder <listname> 
      $list = $web.Lists["<listname>"


      #If the list is found
      if($list)
      {
  
  #Put list item id in placeholder <itemid> 
  $item = $list.GetItemById(<itemid> );


  #Put Item property key that you want to operate upon in placeholder               <propkey> 
  $_ItemBagKey = "<propkey>"


  #Replace placeholder <OP> with type of operation you want to perfrom it            could be one among four values ( ADD, UPDATE , DELETE , VIEW )
  $_Operation  = "<OP>"  # "ADD" # "UPDATE" # "DELETE" # "VIEW"


       #Replace placeholder <propvalue> with new value that you want to update          if you want to delete or view just leave the value blank.
       $_NewValue   = "<propvalue>"
  

 #call function to perform operation
 UpdateKey $_ItemBagKey $_Operation $_NewValue $item


 #Dispose web object to release resources.
 $web.Dispose()

}
else
{
   Write-Host "List not found." -ForegroundColor Red;
}
}
catch
{
   # Add your exception handling routine here to trap and handle the exception details.
}






Thursday, July 23, 2015

Sharepoint hide an item from edit control block (ECB)

A simpler way to remove any item from the Edit Control Block of SharePoint 2013 site is to add some javascript in master page. This way we can also hide the item(s) from specific lists. To do this, just add the below code in a javascript file and link that js file to your master page. Alternatively you can also make use of Content Editor Web part to embed it in a page.


$(document).ready(function ()
{
    $('.ms-core-menu-list').live('focus', function ()
    {
        var ecbToHide = "Version History";
        var showVersionHistory = $("li[text='" + ecbToHide + "']");
        if window.location.href.toLowerCase().indexOf(encodeURIComponent('/Lists/TechPerspect/')) != -1)
        {
            showVersionHistory.hide();
        }
        else
        {
            showVersionHistory.show();
        }
    });

});

The above snippet will remove the ECB item - "Version History" from the List - "TechPerspect".