Friday, 5 April 2019

upload file in document library using pnp and spfx



How to upload file in document library using pnp in SharePoint framework


private UploadFile():void
{
    // to find file uploader control by element id
    let input = <HTMLInputElement>document.getElementById("thefileinput");
    let file = input.files[0];
   if (file.size <= 10485760)
   {
    //upload small file in document library
    pnp.sp.web.getFolderByServerRelativeUrl("/sites/apps/TestDoc").files.add(file.name,file,true).then(f=>
    {
      // use below to update the properties of document
        f.file.getItem().then(item =>{
          item.update({
            FirstName:'test001',
            LastName:'test',
            Age:50
          }).then(f=>{
            alert("File uploaded successfully"+f.data["odata.etag"]);
          });
        });
    });
  }
  else
  {
    //upload large file in document library
      pnp.sp.web.getFolderByServerRelativeUrl("/sites/apps/TestDoc").files.addChunked(file.name,file,data =>
      {
         console.log({ data: data, message: "progress" });
      }, true).then(f=>
      {
              alert("File uploaded successfully"+f.data["odata.etag"]);  
      });
    }
}

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);
});
}