Create Folder in a simple SP.List using REST API

When you create a folder using the Web UI, SharePoint creates two things:

  1. Creates a folder
  2. Creates a list item

By the way, it’s the list item that you see on the Web UI List View.

So, If you need to create a folder in a List, firstly create a folder based on the ContentTypeId. The ContentTypeIdfor a Folder is 0x0120. Note that you only have the Title available at this level.
Then take the created folder and update it with Title and FileLeafRef. If you have additional columns, update them as well.
If you omit this step, your folder will be created with the specific name, but if you click on the item, you will get the ID as the folder name.

Here is my snipped which I use:

var folderName =”myNewFolder”;
var listName=”myList”;
var call = addNewItemAsFolder(folderName, listName);

function addNewItemAsFolder(folderName, listName) {
var itemType=getItemTypeForListName(listName);
var data= {

“__metadata”: { “type”: itemType},
“Title”: folderName,
“ContentTypeId”:”0x0120″ //ContentType for Folder

};

var url= _spPageContextInfo.webAbsoluteUrl+”/_api/Web/Lists/GetByTitle(‘”+listName+”‘)/Items”;

var call = $.ajax({

url: url,
type: “POST”,
headers: {

“accept”: “application/json;odata=verbose”,
“X-RequestDigest”: $(“#__REQUESTDIGEST”).val(),
“content-Type”: “application/json;odata=verbose”

},
data: JSON.stringify(data),
success: function (data) {

//Update the Folder Items
jQuery.ajax({
url: url+”(‘” + data.d.Id + “‘)”,
type: “POST”,

data: JSON.stringify({

“__metadata”: { type: itemType },
Title: folderName,
FileLeafRef: folderName,
myColumn01:  “this is my additional column”,
myColumn02Name: “this is my additional column”

}),

headers: {

Accept: “application/json;odata=verbose”,
“Content-Type”: “application/json;odata=verbose”,
“X-RequestDigest”: jQuery(“#__REQUESTDIGEST”).val(),
“IF-MATCH”: “*”,
“X-Http-Method”: “PATCH”

}

});

},
error: function (error) {
alert(JSON.stringify(error));
}
});
return call;
}

// Get List Item Type metadata
function getItemTypeForListName(name) {
return “SP.Data.” + name.charAt(0).toUpperCase() + name.split(” “).join(“”).slice(1) + “ListItem”;
}

SharePoint Person/Group Column with REST API

With SharePoint REST API you can iterate through all columns. But what happens when you want to display a Person/Group column like the Author? You will get a User ID as a result.

To display the “Authors Full Name” instead of the ID just use the parameter $expand.

var listname = "My List";
var url =  _spPageContextInfo.webAbsoluteUrl + 
  "/_api/web/lists/getbytitle('"+listName+"')/items?
  "$select=Id,Title,Created,Author/Title&amp"+;
  "$top=20&amp"+;
  "$orderby=Created desc&amp"+;
  "$expand=Author";
...
$.ajax({
 url: url ,
 method: "GET",
 headers: {"Accept": "application/json; odata=verbose" },
 success: function (data) {
   if(data.d.results.length >0){
    for (var i = 0; i < data.d.results.length; i++){
     data.d.results[i].Author.Title
    }
   }
 }
...

And if you need to use the filter based on the Person/Group column here is how to declare it:

var urlPI = _spPageContextInfo.webAbsoluteUrl +
       "/_api/web/lists/getbytitle('"+listName+"')/items?"+
       "$select=Author,Author/Title"+
       "$top=5000"+
       "$orderby=Created desc"+
       "$expand=Author&"+
       "$filter=Author eq '"+_spPageContextInfo.userId+"'";

.

SP Workflow Stages

If you are using SharePoint Workflows the system stores a digital value instead of the text value.

Status Value
Not Started 0
Failed on Start 1
In Progress 2
Error Occurred 3
Canceled 4
Completed 5
Failed on Start (retrying) 6
Error Occurred (retrying) 7
Canceled 15
Approved 16
Rejected 17

Current User with SPService

To get the current user info just use the SPServive to retrive all information you need.

var thisUsersValues = $().SPServices.SPGetCurrentUser({
fieldNames: [“Title”, “EMail”, “Office”, “LastName”, “FirstName”, “WorkPhone”],
debug: false
});
console.log(“Display name: “+ thisUsersValues.Title);

SiteManager

SiteManager is still available on SharePoint 2013.

_layouts/sitemanager.aspx

DataViewWebpart in matrix display with XSLT modifications

Starting point:

  • SharePoint document library with documents
  • Documents use Category: Global, Regional and Local
  • Each document use a title. Needed for grouping.
  • Calculated-Column to generate a sorting order

Objective point:

  • Documents should be arrange in a matrix.
  • Header column displays the category
  • One document with the same title will and different categories should be display in one row.

Solution:

  • Using SharePoint Designer
  • Adding new DataView WebPart
  • Modify the XSLT

<xsl:template name=”dvt_1″>
<xsl:variable name=”dvt_StyleName”>2ColFrm</xsl:variable>
<xsl:variable name=”Rows” select=”/dsQueryResponse/Rows/Row” />

<xsl:variable name=”dvt_RowCount” select=”count($Rows)” />
<xsl:variable name=”dvt_IsEmpty” select=”$dvt_RowCount = 0″ />
<xsl:choose>
<xsl:when test=”$dvt_IsEmpty”>
<xsl:call-template name=”dvt_1.empty” />
</xsl:when>
<xsl:otherwise>
<table width=”100%” border=”1px” cellpadding=”0″ cellspacing=”0″>
<tr>
<th style=”padding: 4px;”>Product name</th>
<th style=”padding: 4px;”>Global</th>
<th style=”padding: 4px;”>Regional</th>
<th style=”padding: 4px;”>Local</th>
</tr>

<xsl:call-template name=”dvt_1.body”>
<xsl:with-param name=”Rows” select=”$Rows” />
</xsl:call-template>

</table>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name=”dvt_1.body”>
<xsl:param name=”Rows” />
<xsl:for-each select=”$Rows[@txtIWCOrder != ”]”>
<xsl:sort select=”@txtIWCOrder” />
<xsl:call-template name=”dvt_1.rowview” />
</xsl:for-each>

</xsl:template>

<xsl:template name=”dvt_1.rowview”>
<xsl:variable name=”pos” select=”position()”/>
<!–<xsl:variable name=”previewItemID”><xsl:value-of select=”substring-after(preceding-sibling::*[1]/@txtIWCOrder, ‘#’)”/></xsl:variable>–>
<xsl:variable name=”previewItemName”><xsl:value-of select=”substring-before(preceding-sibling::*[1]/@txtIWCOrder, ‘#’)”/></xsl:variable>
<xsl:variable name=”nextItemName”><xsl:value-of select=”substring-before(following-sibling::*[1]/@txtIWCOrder, ‘#’)”/></xsl:variable>
<!–<xsl:variable name=”nextItemID”><xsl:value-of select=”substring-after(following-sibling::*[1]/@txtIWCOrder, ‘#’)”/></xsl:variable>–>
<xsl:variable name=”currentItemID”><xsl:value-of select=”substring-after(@txtIWCOrder,’#’)”/></xsl:variable>
<xsl:variable name=”currentItemName”><xsl:value-of select=”substring-before(@txtIWCOrder,’#’)”/></xsl:variable>

<xsl:choose>
<xsl:when test=”position()=1″>
<xsl:text disable-output-escaping=”yes”>&lt;tr class=”Beginn”&gt;</xsl:text>
<td style=”padding:4px;”>
<xsl:value-of select=”@Title” disable-output-escaping=”yes”/>
</td>

<xsl:choose>
<xsl:when test=”$currentItemID = 1″>
<td  align=”center”><a href=”{@FileDirRef}/{@FileLeafRef}” title=”{@Title} – {@Category}”>X</a></td>
<xsl:if test=”$nextItemName !=$currentItemName”><td  align=”center”>-</td></xsl:if>
<xsl:if test=”$nextItemName !=$currentItemName”><td  align=”center”>-</td></xsl:if>
</xsl:when>
<xsl:when test=”$currentItemID = 2″>
<td  align=”center”>-</td>
<td  align=”center”><a href=”{@FileDirRef}/{@FileLeafRef}” title=”{@Title} – {@Category}”>X</a></td>
<xsl:if test=”$nextItemName !=$currentItemName”><td  align=”center”>-</td></xsl:if>
</xsl:when>
<xsl:when test=”$currentItemID = 3″>
<td  align=”center”>-</td>
<td  align=”center”>-</td>
<td  align=”center”><a href=”{@FileDirRef}/{@FileLeafRef}” title=”{@Title} – {@Category}”>X</a></td>
</xsl:when>
</xsl:choose>

</xsl:when>
<xsl:when test=”$currentItemName = $nextItemName”>
<!– n-node –>
<xsl:text disable-output-escaping=”yes”>&lt;tr class=”Equal”&gt;</xsl:text>
<td class=”Equal” style=”padding:4px;”>
<xsl:value-of select=”@Title” disable-output-escaping=”yes”/>
</td>
<td class=”multiple-CurNext”  align=”center”><a href=”{@FileDirRef}/{@FileLeafRef}” title=”{@Title} – {@Category}”>X</a></td>

</xsl:when>
<xsl:when test=”$currentItemName != $nextItemName”>
<xsl:if test=”$currentItemName != $previewItemName”>
<xsl:text disable-output-escaping=”yes”>&lt;tr class=”Single”&gt;</xsl:text>
<td style=”padding:4px;”>
<xsl:value-of select=”@Title” disable-output-escaping=”yes”/>
</td>

<xsl:choose>
<xsl:when test=”$currentItemID = 1″>
<td  align=”center”><a href=”{@FileDirRef}/{@FileLeafRef}” title=”{@Title} – {@Category}”>X</a></td>
<xsl:if test=”$nextItemName !=$currentItemName”><td  align=”center”>-</td></xsl:if>
<xsl:if test=”$nextItemName !=$currentItemName”><td  align=”center”>-</td></xsl:if>
</xsl:when>
<xsl:when test=”$currentItemID = 2″>
<td  align=”center”>-</td>
<td  align=”center”><a href=”{@FileDirRef}/{@FileLeafRef}” title=”{@Title} – {@Category}”>X</a></td>
<xsl:if test=”$nextItemName !=$currentItemName”><td  align=”center”>-</td></xsl:if>
</xsl:when>
<xsl:when test=”$currentItemID = 3″>
<td  align=”center”>-</td>
<td  align=”center”>-</td>
<td  align=”center”><a href=”{@FileDirRef}/{@FileLeafRef}” title=”{@Title} – {@Category}”>X</a></td>
</xsl:when>
</xsl:choose>

<xsl:text disable-output-escaping=”yes”>&lt;/tr&gt;</xsl:text>
</xsl:if>

<xsl:if test=”$currentItemName = $previewItemName”>
<!– Only table section headline –>
<xsl:if test=”$currentItemName = $nextItemName”>
<xsl:text disable-output-escaping=”yes”>&lt;tr class=”Multiple”&gt;</xsl:text>
<td style=”padding:4px;”> <xsl:value-of select=”@Title” disable-output-escaping=”yes”/>
</td>
</xsl:if>
<td  class=”multiple” align=”center” ><a href=”{@FileDirRef}/{@FileLeafRef}” title=”{@Title} – {@Category}”>X</a></td>

<xsl:if test=”$currentItemName != $nextItemName”>
<td  class=”multiple-empty” align=”center”>-</td>
</xsl:if>
</xsl:if>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
<xsl:if test=”position() = last()”>
<xsl:text disable-output-escaping=”yes”>&lt;/tr&gt;</xsl:text>
</xsl:if>
</xsl:template>

Create SPGroups with PowerShell

You can create via PowerShell SharePoint Groups for a specific SiteCollection.

$siteURL = “http://intramania.com/sites/mysitecollection
$web = Get-SPWeb $siteURL
$groups = $web.SiteGroups
$groups.Add(“My-Group-Name-01”, $web.CurrentUser, $null, “SharePoint Group Name 01”)
$groups.Add(“My-Group-Name-02”, $web.CurrentUser, $null, “SharePoint Group Name 02”)
$groups.Add(“My-Group-Name-03”, $web.CurrentUser, $null, “SharePoint Group Name 03”)
$groups.Add(“My-Group-Name-04”, $web.CurrentUser, $null, “SharePoint Group Name 04”)
$groups.Add(“My-Group-Name-05”, $web.CurrentUser, $null, “SharePoint Group Name 05”)