Wednesday, 27 March 2019

Delete list item using power shell with batch process

Start-Transcript -path C:\output.txt -append
$SiteUrl ="Site url"
$list =  (Get-Spweb "$SiteUrl").GetList("$SiteUrl"+/Lists/ListTitle/)
$query = New-Object Microsoft.SharePoint.SPQuery;
$query.ViewAttributes = "Scope='Recursive'";
$query.RowLimit = 2000;
$query.Query = '<Where><Leq><FieldRef Name="Created"/><Value Type="DateTime" IncludeTimeValue="TRUE">YYYY-MM-DDT00:00:00Z</Value></Leq></Where>';
#$query.Query = '';

$itemCount = 0;
$listId = $list.ID;
[System.Text.StringBuilder]$batchXml = New-Object "System.Text.StringBuilder";
$batchXml.Append("<?xml version=`"1.0`" encoding=`"UTF-8`"?><Batch>");
$command = [System.String]::Format( "<Method><SetList>{0}</SetList><SetVar Name=`"ID`">{1}</SetVar><SetVar Name=`"Cmd`">Delete</SetVar></Method>", $listId, "{0}" );
Write-Host "before do while "
Get-Date
do
{
    $listItems = $list.GetItems($query)
    $query.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
    foreach ($item in $listItems)
    {
        if($item -ne $null){$batchXml.Append([System.String]::Format($command, $item.ID.ToString())) | Out-Null;$itemCount++; write-host "Created on " $item["Created"] $itemCount }
    }
}
while ($query.ListItemCollectionPosition -ne $null)

$batchXml.Append("</Batch>");
$itemCount;
Write-Host "after do while "
Get-Date


#And lastly (and most importantly!), run the query
#Write-Host "Batch process started "
#Get-Date
#$web = Get-Spweb $SiteUrl;
#$web.ProcessBatchData($batchXml.ToString()) | Out-Null;
#Write-Host "Batch process end"
#Get-Date

Stop-Transcript

Tuesday, 26 March 2019

Delete SharePoint list items using powershell with caml query

Add-PSSnapin Microsoft.SharePoint.PowerShell

[System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$web = Get-SPWeb "SiteURL"
$list = $web.Lists["OTSubmitForm"]
$DeleteBeforeDate = [Microsoft.SharePoint.Utilities.SPUtility]::CreateISO8601DateTimeFromSystemDateTime([DateTime]::Now.AddDays(-37))
$caml='<Where> <Lt> <FieldRef Name="Created" /><Value Type="DateTime">2016-05-30T11:12:56Z</Value> </Lt> </Where>'
$query=new-object Microsoft.SharePoint.SPQuery
$query.Query=$caml
$col=$list.GetItems($query)
Write-Host $col.Count
$col | % {$list.GetItemById($_.Id).Delete()}
$web.Dispose()

Friday, 1 March 2019

Sharepoint Sandbox Service issue - UserCodeExecutionProxy call failed. No connection could be made because the target machine actively refused it SERVERIP:32846



Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Windows>cd %windir%\system32\

C:\Windows\system32>lodctr /R

Info: Successfully rebuilt performance counter setting from system backup store
C:\Windows\system32>cd %windir%\sysWOW64\

C:\Windows\SysWOW64>lodctr /R

Info: Successfully rebuilt performance counter setting from system backup store
C:\Windows\SysWOW64>


Start => Run->Services.msc :  SharePoint 2010 User Code Host ( Start Service) 

Thursday, 24 January 2019

Tuesday, 4 December 2018

Encode - decode base64



https://scotch.io/tutorials/how-to-encode-and-decode-strings-with-base64-in-javascript

Thursday, 1 November 2018

Remove Duplicate Values in SharePoint List

<!--Copy all source code, paste it inside Content Editor /  Script Editor-->

<div><h1>Remove Duplicate Values</h1></div>
<div>Enter Your List Name : <input type='text' id='txtList' /></div>
<div>Enter Your Unique Column : <input type='text' id='txtUniColumn' /><input type='button' value='Remove Duplicate Value' onclick='getListItems()'/></div>

<div id='divDel'>Deleted Items
<div id='divDelItems'></div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type='text/javascript'>

$(document).ready(function(){
debugger;
$('#divDelItems').html('');
$('#divDel').hide();
});

function RemoveDuplicateItems(items, propertyName) {
    var result = [];
     var ExistingItem = [];
    if (items.length > 0) {
        $.each(items, function (index, item) {
            if ($.inArray(item[propertyName], result ) == -1) {
                    if(!IsNullOrUndefined(item[propertyName])){
                             result.push(item[propertyName]);
                    }
            }
            else{
                   if(!IsNullOrUndefined(item[propertyName])){
                            ExistingItem.push(item['Id']+ ' : ' + item[$('#txtUniColumn').val()] );
                   }
           }
        });
    }
    return ExistingItem;
}



function getListItems() {
    $.ajax({
        url:  _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('"+$('#txtList').val()+"')/items?$select="+$('#txtUniColumn').val()+",Id&$top=5000&$orderby= Modified desc" ,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function(data) {

            var items = data.d.results;
             items = RemoveDuplicateItems(items, $('#txtUniColumn').val());
             $('#divDel').show();
             if(items!=undefined && items.length>0){
             for(var i=0; i<items.length; i++){
                   deleteItems(items[i]);
            }}
        },
        error: function(data) {
            alert(data.responseText);
        }
    });
}
var count = 0;
function deleteItems(Id){
  $.ajax({ 
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('"+$('#txtList').val()+"')/items(" + Id.split(':')[0] + ")", 
            type: "POST", 
            contentType: "application/json;odata=verbose", 
            headers: { 
                "Accept": "application/json;odata=verbose", 
                "X-RequestDigest": $("#__REQUESTDIGEST").val(), 
                "IF-MATCH": "*", 
                "X-HTTP-Method": "DELETE", 
            }, 
            success: function(data) { 
                   var cnt = count++;
                  console.log(Id + ' Count - ' );
                  $('#divDelItems').append('<div>' + Id + ' Count - ' + cnt  + '</div>');
            }, 
            error: function(data) { 
                 
            } 
        });
}

</script>