Blog

Powershell Get SPList Last Modified

On 11/10/2018

param($webUrl="http://fdi-sp2013-1:2921", [string]$listTitle="Pages")
 
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
clear;
$web = Get-SPWeb $webUrl
$list = $web.Lists[$listTitle]
$list.PropertiesXml
 
$props = New-Object System.Xml.XmlDocument
$props.LoadXml($list.PropertiesXml)
 
$props.ChildNodes
 
$web.Dispose()
 
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');

get script

Listlastmodified

Sharepoint Hide List With REST

On 10/10/2018

function UpdateList(){
var listName="Acquisition";//set here liste title
var list = {
"__metadata": { "type": 'SP.List' },
"Hidden": true//Set here visibility : true == hidden
};
 
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(list),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "MERGE",
"If-Match": "*"
},
success: function (data) {
alert('Success');
window.location.href=window.location.href;//reload the page
},
error: function (data) {
console.dir(data);
}
});
}
UpdateList();
 
 

REST get Lists From Current Web

On 08/10/2018

function getlists() {
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists",
method: "GET",
headers: {
"Accept": "application/json; odata=verbose"
},
success: function (data) {
var lists = data.d.results;
console.clear();
console.dir(lists);
for (var i = 0 ; i < lists.length ; i++) {
var lst = lists[i];
//console.dir(lst);
$.ajax({
async: false,
headers: { "accept": "application/json; odata=verbose" },
method: "GET",
url: lst.RootFolder.__deferred.uri,
success: function (data) {
// get the rootFolder
var folder = data.d;
console.log("Title : " + lst.Title + "; RootFolder :" + folder.ServerRelativeUrl
+ "; Hidden :" + lst.Hidden + "; ItemCount :" + lst.ItemCount + "; LastItemModifiedDate :" + lst.LastItemModifiedDate
+ "; BaseTemplate :" + lst.BaseTemplate);
}
});
 
}
},
error: function (data) {
console.log(data);
}
});
}
get the code only

Visual Studio Push Dll to Gac on Build Succeded

On 05/10/2018

push your dll in gac on build succeded

In your projet setiings click on Build Events, in post buil events add command below

"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\gacutil.exe" /if "$(TargetPath)"

Add task to reset sharepoint search index with powershell

On 05/10/2018

Create a powershell script (task1.ps1) with code below


if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell -Verbose -ErrorAction:Stop
}
#the $True parameter denotes: Disable Alerts and Ignore Timeout error.
(Get-SPEnterpriseSearchServiceApplication).reset($true, $true)

Add a new task in your task scheduler

Check the radio button 'Run whether user is logged on or not

Set a name and add a trigger to set scheduling

set the action : program / script

	
		%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
	
	

argument with -command followed by the pass to your powershell script

	
		-executionpolicy unrestricted -command D:\PS1\Task1.ps1
	
	

Save your task

 

reset indexes of one content source only

PowerShell Export List Fields To Xml

On 01/10/2018

param([string]$webUrl=http://fdi-sp2013-1,
[string]$listUrl="Lists/myList",
[string]$contentTypeName,
[boolean]$includeHiddenFields=$true,
[string]$excludeTypes="Computed;Lookup;Guid"
)
clear
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Push-Location
 
function AddType($typeAsString)
{
$types = $excludeTypes.Split(";");
return $types.Contains($typeAsString) -eq $true;
}
function AddField($field)
{
<##>
if(AddType($field.TypeAsString) -eq $false)
{
return;
}
 
$fields = $script:exportNode.SelectSingleNode("/List/Fields");
[System.XML.XMLElement]$oXMLField=$global:exportNode.CreateElement("Field");
#InternalName
$urlAt = $global:exportNode.CreateAttribute("InternalName");
$urlAt.Value = $field.InternalName;
$oXMLField.Attributes.Append($urlAt);
$fields.AppendChild($oXMLField);
#Title
$urlAt = $global:exportNode.CreateAttribute("Title");
$urlAt.Value = $field.Title;
$oXMLField.Attributes.Append($urlAt);
$fields.AppendChild($oXMLField);
#TypeAsString
$urlAt = $global:exportNode.CreateAttribute("TypeAsString");
$urlAt.Value = $field.TypeAsString;
$oXMLField.Attributes.Append($urlAt);
$fields.AppendChild($oXMLField);
#Hidden
$urlAt = $global:exportNode.CreateAttribute("Hidden");
$urlAt.Value = $field.Hidden;
$oXMLField.Attributes.Append($urlAt);
$fields.AppendChild($oXMLField);
#Required
$urlAt = $global:exportNode.CreateAttribute("Required");
$urlAt.Value = $field.Required;
$oXMLField.Attributes.Append($urlAt);
#Group
$urlAt = $global:exportNode.CreateAttribute("Group");
$urlAt.Value = $field.Group;
$oXMLField.Attributes.Append($urlAt);
$fields.AppendChild($oXMLField);
<# #>
}
 
$web = Get-SPWeb $webUrl -ErrorAction:SilentlyContinue;
if($web -eq $null)
{
read-Host "cannot access to $($webUrl) push any key to exit"
exit 0;
}
Write-Host "connected to $($webUrl) Title $($web.Title)"
 
$list = $web.GetList([Microsoft.Sharepoint.Utilities.SPUtility]::ConcatUrls($web.ServerRelativeUrl, $listUrl));
Write-Host "list $($list.Title) loaded"
 
#create xml document
$global:exportNode = New-Object System.Xml.XmlDocument
 
#add list node
[System.XML.XMLElement]$oXMLRoot=$global:exportNode.CreateElement("List")
 
 
#web Url attribute
$urlAt = $global:exportNode.CreateAttribute("url");
$urlAt.Value = $webUrl;
$oXMLRoot.Attributes.Append($urlAt);
 
#web title attribute
$urlAt = $global:exportNode.CreateAttribute("webTitle");
$urlAt.Value = $web.Title;
$oXMLRoot.Attributes.Append($urlAt);
 
#List Title attribute
$urlAt = $global:exportNode.CreateAttribute("ListTitle");
$urlAt.Value = $list.Title;
$oXMLRoot.Attributes.Append($urlAt);
 
#List Url attribute
$urlAt = $global:exportNode.CreateAttribute("ListUrl");
$urlAt.Value = $listUrl;
$oXMLRoot.Attributes.Append($urlAt);
 
#add list note to xmlDocument
$global:exportNode.appendChild($oXMLRoot);
 
$fields = $null;
 
if($includeHiddenFields)
{
$fields = $list.Fields |Sort-Object{$_.InternalName} | select Title, InternalName, TypeAsString, Hidden, Required, Group
}
else
{
$fields = $list.Fields |Sort-Object{$_.InternalName} | Where-Object {$_.Hidden -eq $false} | select Title, InternalName, TypeAsString, Hidden, Required, Group
}
 
#add fields
[System.XML.XMLElement]$oXMLFields=$global:exportNode.CreateElement("Fields")
$global:exportNode.FirstChild.AppendChild($oXMLFields);
foreach($field in $fields){
AddField($field);
}
 
#add ContentTypes
[System.XML.XMLElement]$oXMLContentTypes=$global:exportNode.CreateElement("ContentTypes")
$global:exportNode.FirstChild.AppendChild($oXMLContentTypes);
 
$oXMLContentTypes = $script:exportNode.SelectSingleNode("/List/ContentTypes");
 
foreach($contentType in $list.ContentTypes)
{
[System.XML.XMLElement]$oXMLContentType=$global:exportNode.CreateElement("ContentType");
$urlAt = $global:exportNode.CreateAttribute("Name");
$urlAt.Value = $contentType.Name;
$oXMLContentType.Attributes.Append($urlAt);
 
$oXMLContentTypes.AppendChild($oXMLContentType);
 
 
[System.XML.XMLElement]$oXMLCTFields=$global:exportNode.CreateElement("Fields");
$oXMLContentType.AppendChild($oXMLCTFields);
<##>
if($includeHiddenFields)
{
$fields = $contentType.Fields |Sort-Object{$_.InternalName} | select Title, InternalName, TypeAsString, Hidden, Required, Group
}
else
{
$fields = $contentType.Fields |Sort-Object{$_.InternalName} | Where-Object {$_.Hidden -eq $false} | select Title, InternalName, TypeAsString, Hidden, Required, Group
}
 
 
#fields in content type
foreach($field in $fields)
{
[System.XML.XMLElement]$oXMLCTField=$global:exportNode.CreateElement("Field");
#InternalName
$urlAt = $global:exportNode.CreateAttribute("InternalName");
$urlAt.Value = $field.InternalName;
$oXMLCTField.Attributes.Append($urlAt);
#Title
$urlAt = $global:exportNode.CreateAttribute("Title");
$urlAt.Value = $field.Title;
$oXMLCTField.Attributes.Append($urlAt);
#TypeAsString
$urlAt = $global:exportNode.CreateAttribute("TypeAsString");
$urlAt.Value = $field.TypeAsString;
$oXMLCTField.Attributes.Append($urlAt);
#Hidden
$urlAt = $global:exportNode.CreateAttribute("Hidden");
$urlAt.Value = $field.Hidden;
$oXMLCTField.Attributes.Append($urlAt);
#Required
$urlAt = $global:exportNode.CreateAttribute("Required");
$urlAt.Value = $field.Required;
$oXMLCTField.Attributes.Append($urlAt);
#Group
$urlAt = $global:exportNode.CreateAttribute("Group");
$urlAt.Value = $field.Group;
$oXMLCTField.Attributes.Append($urlAt);
 
$oXMLCTFields.AppendChild($oXMLCTField);
 
}
}
<# #>
#export to test file
$global:exportNode.OuterXml | Out-File "$($list.Title.Replace(" ", "_")).xml" -Encoding utf8
 
#dispose the web
$web.Dispose()

download file

Display Spfield from SPList

On 29/09/2018

function getListFields() {
var listTitle = "Pages";
var showHidden = false;
var outPut = $("#fdiOutput");
var addOutput = outPut != null && (outPut.length > 0)? true : false;
var tbl = "<table class='fdi-table'>";
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('" + listTitle + "')/Fields",
type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose"
},
success: function (response) {
console.log(response);
if (addOutput)
tbl += "<tr><th>InternalName</th><th>Hidden</th><th>TypeAsString</th><th>Title</th></tr>";
for (var i = 0 ; i < response.d.results.length ; i++) {
var field = response.d.results[i];
if (!showHidden && field.Hidden) {
continue;
}
console.log(field.InternalName + " ; " + field.Hidden + " ; " + field.TypeAsString + " ; " + field.DisplayName);
if (addOutput)
tbl += '<tr><td>' + field.InternalName + '</td><td>' + field.Hidden + '</td><td>' + field.TypeAsString + '</td><td>' + field.Title + '</td></tr>';
}
tbl += "</table>";
if (addOutput)
$("#fdiOutput").append(tbl);
},
error: function (data) {
console.log("error getListFields " + listTitle);
console.log(data);
debugger;
}
});
}

get script

Sharepoint PowerShell query search service

On 29/09/2018

SharePoint PowerShell query search service

param([string]$siteCollectionUrl="http://mySiteUrl" ,
$keyword="ContentTypeId:0x010100020D149374FFC4DF792F30EFC030C9D01* ",
$rowLimit=500,
$SelectProperties="Title,SPWebUrl,Path"
)
 
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
$site = Get-SPSite $siteCollectionUrl
$keywordQuery = New-Object Microsoft.Office.Server.Search.Query.KeywordQuery($site);
 
#set request
$keywordQuery.QueryText = $keyword
 
#set managed properties to retreive
foreach($mnp in $SelectProperties.Split(","))
{
$keywordQuery.SelectProperties.Add($mnp.Trim());
}
$keywordQuery.RowLimit = $rowLimit
 
$searchExec = New-Object Microsoft.Office.Server.Search.Query.SearchExecutor
$searchResults = $searchExec.ExecuteQuery($keywordQuery)
 
#display results
$table = $searchResults.Table
$table | Select Title, SPWebUrl, Path
 
$site.Dispose()