Showing posts with label SharePoint Online. Show all posts
Showing posts with label SharePoint Online. Show all posts

Thursday, March 18, 2021

How to list all sites and subsites in SharePoint Online using PowerShell?

function Get-SPOWebs(){

param(

   $Url = $(throw "Please provide a Site Collection Url"),

   $Credential = $(throw "Please provide a Credentials")

)


  $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)  

  $context.Credentials = $Credential 

  $web = $context.Web

  $context.Load($web)

  $context.Load($web.Webs)

  $context.ExecuteQuery()

  foreach($web in $web.Webs)

  {

       Get-SPOWebs -Url $web.Url -Credential $Credential 

       $web

  }

}

Thursday, January 7, 2021

Get all sites and all sub sites using SharePoint online REST API?

 Created Add-in on SharePoint instance with following permission xml

<AppPermissionRequests>

        <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl"/>

        <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="Read"/>

</AppPermissionRequests>


Used below API to get all sites and subsites

http://devprasad007-admin.sharepoint.com/_api/search/query?querytext='contentclass:STS_Site' &rowlimit=100


https://<site_name>.sharepoint.com/_api/search/query?querytext='contentclass:STS_Site 

Path:"https://<site_name>.sharepoint.com/*"'&rowlimit=500 

Change permissions of a SharePoint list using JavaScript?

Break role inheritance for a List object and grant Full Control permissions for a current user



 var listTitle = 'Documents';

var spCtx = SP.ClientContext.get_current();

var list = spCtx.get_web().get_lists().getByTitle(listTitle);   


spCtx.load(list,'HasUniqueRoleAssignments'); 

spCtx.executeQueryAsync(

   function(){

      var hasUniqueAssgns = list.get_hasUniqueRoleAssignments();

      if(!hasUniqueAssgns) {

         list.breakRoleInheritance(false, true);

         spCtx.executeQueryAsync(

            function(){

                console.log('Success');

            }, 

            function(sender,args){

               console.log(args.get_message());    

            }

         );

      }

   }, 

   function(sender,args){

      console.log(args.get_message());    

   }

);

Web.GetFileByServerRelativeUrl throws “Value does not fall within expected range”?

 SP Online site stored in Documents, When we trying to adding/retrieving documents but in the delete flow If you get an error during retrieval of a File object.


var relativeUrl = "/sites/documentsite/Documents/images.jpg";
we have to specific or construct the full URL.

Get file url from SharePoint by name

 List docs = web.Lists.GetByTitle("DOCUMENTS");

Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);

clientContext.ExecuteQuery();


clientContext.Load(uploadFile.ListItemAllFields, item => item["EncodedAbsUrl"]);

clientContext.ExecuteQuery();


var fileUrl = uploadFile.ListItemAllFields["EncodedAbsUrl"].ToString();


string link = ctx.Web.CreateAnonymousLinkForDocument(fileUrl, ExternalSharingDocumentOption.View);


string linkwithExpiration = ctx.Web.CreateAnonymousLinkWithExpirationForDocument(fileUrl, ExternalSharingDocumentOption.Edit, DateTime.Now.AddMonths(1));


SharingResult result = ctx.Web.ShareDocument(fileUrl, "reven@peakfinders.com", ExternalSharingDocumentOption.View, true, "Documents shared Sucessfully");