Monday, 25 March 2019

Save As Template Communication Site

While Creating Template from Communication Site ,You may get access denied error. To resolve the error need to do some workaround by powershell using tenant admin credential


Cause:  Feature is disabled on tenant level

When you access /_layouts/savetmpl.aspx , access denied may occur. It happens because the denyaddandcustomizepages of this site is true. DenyAddAndCustomizePages determines whether the Add And Customize Pages right is denied on the site collection.



Run below command with tenant credential or who has admin rights on admin site collection

$adminUPN="user@tenant.onmicrosoft.com"
$userCredential = Get-Credential -UserName $adminUPN -Message "Type the password."

Connect-SPOService -Url https://tenant-admin.sharepoint.com -Credential $userCredential
set-sposite https://tenant.sharepoint.com/sites/communication -denyaddandcustomizepages $false

Tuesday, 19 March 2019

spfx pnp orderby sharepoint list item




How to orderBy SPList Item using pnp


private GetSelectedColumn():void
{
pnp.sp.web.lists.getByTitle("Employee").items.orderBy("Name",true)
.get().then(function(response){
console.log(response);
}).catch(function(exception){
alert("Something went wrong :"+exception);
});
}

spfx pnp select columns



How to get only selected columns using pnp


private GetSelectedColumn():void
{
pnp.sp.web.lists.getByTitle("Employee").items.select("Name","Id").get()
.then(function(response){
console.log(response);
}).catch(function(exception){
alert("Something went wrong :"+exception);
});
}

pnp SPListItem filter


How to filter SharePoint List Item using pnp.


private SPListItemFilter():void
{
pnp.sp.web.lists.getByTitle("Employee").items
.filter("Name eq 'Mohd Muqtdeer'").get().then(function(response){
console.log(response);
}).catch(function(exception){
alert("Something went wrong :"+exception);
});

}

Configure properties in pnp



Below is the example how can we change the baseurl or other properties in pnp and connect to specific web using pnp in spfx


private pnpSPConfiguration():void
{
let headerOption:any=["Accept","application/json;odata=verbose"];
pnp.sp.configure(headerOption,this.context.pageContext.web.absoluteUrl+"/demo")
.web.lists.get().then(function(response)
{
console.log(response);
});
}

spfx commands



Below are some basic commands to work with SharePoint framework with visual studio code tool.




//to create new solution
md SPHellowWebpart
cd SPHellowWebpart
yo @microsoft/sharepoint

//to add new webpart/Extension
//start new terminal and
//run below command
yo @microsoft/sharepoint

//to start the host on local machine
gulp serve

//to trust the certificate on local host
gulp trust-dev-cert

//to install libary/package
npm install libararyName

gulp serve --nobrowser // if already runing local host
gulp budle --ship //before package run this command for dev without ship
gulp package-solution --ship //to create package run this command for dev without
ship



Sunday, 17 March 2019

Batch Operation using pnp


private BatcOperation():void
{
let batchResults = [];
let batch=pnp.sp.createBatch();
pnp.sp.web.lists.getByTitle("Test").items.inBatch(batch).add({Title:"1"})
.then(function(response){
console.log(1);
batchResults.push(response);
});
pnp.sp.web.lists.getByTitle("Test").items.inBatch(batch).add({Title:"2"})
.then(function(response){
console.log(2);
batchResults.push(response);
});;
pnp.sp.web.lists.getByTitle("Test").items.inBatch(batch).add({Title:"3"})
.then(function(response){
console.log(3);
batchResults.push(response);
});;
pnp.sp.web.lists.getByTitle("Test").items.inBatch(batch).add({Title:"4"})
.then(function(response){
console.log(4);
batchResults.push(response);
});;
batch.execute().then(function(){
console.log("Final");
console.log(batchResults);
}).catch(function(exception){
alert("Something went wrong :"+exception);
});

}

spfx Get Files and Folder using pnp



private GetFilesFolder():void
{
pnp.sp.web.lists.getByTitle("Site Assets").items.getAll().then(function(response)
{
console.log(response);

}).catch(function(exception)
{
alert("Something went wrong :"+exception);
});
}

spfx pnp delete folder


private DeleteFolder():void
{
//id of content type
pnp.sp.web.getFolderByServerRelativePath("SiteAssets/NewFolder").delete()
.then(function(response)
{
console.log("Folder Deleted !");
}).catch(function(exception)
{
alert("Something went wrong :"+exception);
});
}

spfx pnp add folder



private AddNewFolder():void
{
//id of content type
pnp.sp.web.folders.add("SiteAssets/NewFolder").then(function(response)
{
console.log("New Folder added !");
}).catch(function(exception)
{
alert("Something went wrong :"+exception);
});
}

spfx Add Content type to List/Library using pnp


private AddSiteToContentTypeToList():void
{
//id of content type
pnp.sp.web.lists.getByTitle("Test").contentTypes
.add("0x01002189FE08454A7F4ABA16A6F0D0E030FE","CustomContentType")
.then(function(response)
{
console.log("Content Type added to List!");
}).catch(function(exception)
{
alert("Something went wrong :"+exception);
});
}

spfx pnp create site content type



private CreateContentType():void
{
pnp.sp.site.rootWeb.contentTypes.add("CustomContentType","CustomContentType")
.then(function(response){
console.log("New Content Type created!");
}).catch(function(exception)
{
alert("Something went wrong :"+exception);
});
}

Add Site Columns using pnp



private AddSiteColumn():void
{
pnp.sp.site.rootWeb.fields.addText("DepartmentName").then(function(response){
console.log("New Site Column Added!");
}).catch(function(exception)
{
alert("Something went wrong :"+exception);
});
}

spfx delete list column using pnp

private DeleteSPListColumn():void
{

pnp.sp.web.lists.getByTitle("Test").fields.getByTitle("EmployeeName")
.delete().then(function(response){
console.log("Column Deleted!");
}).catch(function(exception)
{
alert("Something went wrong :"+exception);
});

}

Add List Column using pnp


private AddSPListColumn():void
{

pnp.sp.web.lists.getByTitle("Test").fields.addText("EmployeeName")
.then(function(response){
console.log("New Column added!");
}).catch(function(exception)
{
alert("Something went wrong :"+exception);
});

}

Delete SPList using pnp



private DeleteSPList():void
{
pnp.sp.web.lists.getByTitle("Employee").delete().then(function(response)
{
console.log("SharePoint List Deleted");
}).catch(function(exception)
{
alert("Something went wrong :"+exception);
});
}

Add SPList using pnp Sharepoint framework



private AddSPList():void
{
pnp.sp.web.lists.add("Test","Test lists").then(function(response){
console.log("SPList added successfully !");
}).catch(function(exception)
{
alert("Something went wrong :"+exception);
});;
}

Break Permission on ListItem using pnp




private BreakInheritPermissionListItem():void
{
pnp.sp.web.lists.getByTitle("Employee").items.getById(9)
.breakRoleInheritance(true).then(function(response)
{
console.log("Item Permission breaked successfully");
}).catch(function(exception)
{
alert("Something went wrong :"+exception);
});
}

Lists spfx breakRoleInheritance using pnp



private BreakInheritPermissionOnSPList():void
{
pnp.sp.web.lists.getByTitle("Employee").breakRoleInheritance(true)
.then(function(response)
{
console.log("Permission breaked successfully");
}).catch(function(exception)
{
alert("Something went wrong :"+exception);
});
}

SiteGroups using pnp



private GetSPSiteGroup():void
{

pnp.sp.web.siteGroups.get().then(function(response)
{
console.log(response);
for(let index:number=0;index<response.length;index++)
{
console.log("Title :"+response[index].Title);
}
}).catch(function(exception)
{
alert("Something went wrong :"+exception);
});
}

spfx Siteusers using pnp



private GetSiteUsers():void
{
pnp.sp.web.siteUsers.get().then(function(response){

console.log(response);
for(let index:number=0;index<response.length;index++)
{
console.log("Title :"+response[index].Title+
" LoginName:"+response[index].LoginName+" Email:"+response[index].Email);
}


}).catch(function(exception){

alert("Something went wrong :"+exception);
})

}

SPList fields using pnp

private GetSPListFields():void
{
pnp.sp.web.lists.getByTitle("Employee").fields.get().then(function(response){

for(let index:number=0;index<response.length;index++)
{
//Show title
console.log("InternalName :"+response[index].InternalName+" StaticName:"+response[index].StaticName+" Title:"+response[index].Title);
}

}).catch(function(exception){
alert("Something went wrong :"+exception);
});
}

SharePoint Lists using pnp



private GetLists():void
{
pnp.sp.web.lists.get().then(function(response){
for(let index:number=0;index<response.length;index++)
{
//Show title
console.log(response[index].Title);
}
});
}

SPfx Crud operation using pnp

import { Version } from '@microsoft/sp-core-library';
import {
BaseClientSideWebPart,
IPropertyPaneConfiguration,
PropertyPaneTextField,
PropertyPaneDropdown,
PropertyPaneToggle,
PropertyPaneSlider
} from '@microsoft/sp-webpart-base';
import { escape } from '@microsoft/sp-lodash-subset';
import { SPHttpClient, SPHttpClientResponse } from '@microsoft/sp-http';
import * as jQuery from 'jquery';
import * as pnp from 'sp-pnp-js';
import 'jqueryui';
import{IListItem} from './IListItem';

import styles from './HellowWebpartWebPart.module.scss';
import * as strings from 'HellowWebpartWebPartStrings';

export interface IHellowWebpartWebPartProps {
description: string;
dropdown:string;
}
export interface ISPList {
Id: number;
Title: string;
Salary: string;
Company:string;
Department:string;
}
export default class HellowWebpartWebPart extends BaseClientSideWebPart<IHellowWebpartWebPartProps> {

public render(): void {
this.domElement.innerHTML =`
<div class="${ styles.container }">
<div class="${ styles.container }">
<div class="${ styles.row }">
<div class="${ styles.column }">
<span class="${ styles.title }">CRUD operations</span>
<p class="${ styles.subTitle }">No Framework</p>
<div class="${ styles.container }">
<div class="${ styles.row }">
<div class="${ styles.column }">Item id</div><div class="${ styles.column }"><input type="text" class="${styles.description} itemid"></div>
</div>
<div class="${ styles.row }">
<div class="${ styles.column }">Name</div><div class="${ styles.column }"><input type="text" class="${styles.description} name"></div>
</div>
<div class="${ styles.row }">
<div class="${ styles.column }">Salary</div><div class="${ styles.column }"><input type="text" class="${styles.description} salary"></div>
</div>
<div class="${ styles.row }">
<div class="${ styles.column }">Company</div><div class="${ styles.column }"><input type="text" class="${styles.description} company"></div>
</div>
<div class="${ styles.row }">
<div class="${ styles.column }">Department</div><div class="${ styles.column }"><input type="text" class="${styles.description} department"></div>
</div>
</div>
<br/>
<div class='itemTable'>
</div>
<br/>
<div class="ms-Grid-row ms-bgColor-themeDark ms-fontColor-white ${styles.row}">
<div class="ms-Grid-col ms-u-lg10 ms-u-xl8 ms-u-xlPush2 ms-u-lgPush1">
<button class="${styles.button} create-Button">
<span class="${styles.label}">Create item</span>
</button>
<button class="${styles.button} read-Button">
<span class="${styles.label}">Read item</span>
</button>
<button class="${styles.button} update-Button">
<span class="${styles.label}">Update item</span>
</button>
<button class="${styles.button} delete-Button">
<span class="${styles.label}">Delete item</span>
</button>
</div>
</div>
</div>
</div>
</div>
</div>`;
this.setButtonsEventHandlers();

}
private setButtonsEventHandlers(): void
{
const webPart: HellowWebpartWebPart = this;
jQuery(".create-Button").click(function()
{
webPart.createItem();
});
jQuery(".update-Button").click(function()
{
webPart.updateItem();
});
jQuery(".delete-Button").click(function()
{
webPart.deleteItem();
});
jQuery(".read-Button").click(function()
{
webPart.readItem();
});
webPart.readItem();
}
private createItem(): void
{
const webPart: HellowWebpartWebPart = this;
pnp.sp.web.lists.getByTitle("Employee").items.add(
{
Title:jQuery('.name').val(),
Salary:jQuery('.salary').val(),
Company:jQuery('.company').val(),
Department:jQuery('.department').val()
}).then(function(response)
{
webPart.readItem();
alert("Item Added Succes fully")
}).catch(function(exception){

alert("Something went wrong :"+exception);
});
}
private readItem(): void
{
const webPart: HellowWebpartWebPart = this;
pnp.sp.web.lists.getByTitle("Employee").items.getAll().then(function(response)
{
let items: ISPList[]=response;
let html: string = '<table class="TFtable" border=1 width=style="bordercollapse: collapse;">';
html += `<th></th><th>ProfileId</th><th>Name</th><th>Salary</th><th>Company</th><th>Department</th>`;
if (items.length>0)
{
items.forEach((item: ISPList) => {
html += `
<tr>
<td><input type="radio" id="ProfileId" class='profileid' name="ProfileId" value="${item.Id}"> <br> </td>
<td>${item.Id}</td>
<td>${item.Title}</td>
<td>${item.Salary}</td>
<td>${item.Company}</td>
<td>${item.Department}</td>
</tr>
`;
});
}
else
{
html +="No records...";
}
html += `</table>`;
jQuery('.itemTable').html(html);
jQuery('.profileid').click(function()
{
webPart.readListItemById(jQuery(this).val());
});

}).catch(function(exception){
alert("Something went wrong :"+exception);
});
}
private readListItemById(itemid:number):void
{
pnp.sp.web.lists.getByTitle("Employee").items.getById(itemid).get().then(function(response)
{
let item:ISPList=response;
jQuery('.itemid').val(item.Id),
jQuery('.name').val(item.Title),
jQuery('.salary').val(item.Salary),
jQuery('.company').val(item.Company),
jQuery('.department').val(item.Department)
}).catch(function(exception){
alert("Something went wrong :"+exception);
});
}
private updateItem(): void
{
const webPart: HellowWebpartWebPart = this;
pnp.sp.web.lists.getByTitle("Employee").items.getById(jQuery('.itemid').val()).update(
{
Title:jQuery('.name').val(),
Salary:jQuery('.salary').val(),
Company:jQuery('.company').val(),
Department:jQuery('.department').val()
}).then(function(response)
{
webPart.readItem();
alert("Item updated Succesfully");
}).catch(function(exception){

alert("Something went wrong :"+exception);
});


} private deleteItem(): void
{
const webPart: HellowWebpartWebPart = this;
pnp.sp.web.lists.getByTitle("Employee").items.getById(jQuery('.itemid').val()).delete().then(function(response)
{
webPart.readItem();
alert("Item deleted Succesfully");
}).catch(function(exception){
alert("Something went wrong :"+exception);
});;
}
protected get dataVersion(): Version {
return Version.parse('1.0');
}


protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
return {
pages: [
{
header: {
description: strings.PropertyPaneDescription
},
groups: [
{
groupName: strings.BasicGroupName,
groupFields: [
PropertyPaneTextField('description', {
label: strings.DescriptionFieldLabel
}),
PropertyPaneDropdown('dropdown', {
label: "City Name",
options:[{key:'1',text:'Saharanpur'},
{key:'2',text:'Banglore'},
{key:'3',text:'Meerut'},
{key:'4',text:'New Delhi'},
{key:'5',text:'Agra'}]
}),
PropertyPaneToggle('toggle',{
label:"Toggle button"
}),
PropertyPaneSlider('Slider',{
label:"Slider",min:1,max:10
})
]
}
]
}
]
};
}
}