Office 365 guide series – Manage files and folders with PowerShell and CSOM


 Office365logo       SP2013logo

How to manage files and folders with PowerShell and CSOM

DocLib1

How can we manage these items…?

This is a pure guide to using PowerShell to manage and manipulate files and folders, libraries and all document management related tasks in a SharePoint Online or OneDrive for Business environment.

The sections in this guide are:

– Prerequisites
– Load assemblies
– Load a CSOM Context
– Web
– List/Library
– GetFileByServerRelativeUrl and GetForlderByServerRelativeUrl
– Create a file from a local copy
– Create a folder from a local copy
– Set properties on a file
– Set properties on a folder
– ResolveUser (Function)
– GetItemProperties (Function)

Prerequisites

Before beeing able to do much in SharePoint Online or OneDrive for Business, you have to start using CSOM, or Client Side Object Model, this allows us to do pretty much everything we could do before using regular PowerShell and the SharePoint CMD’lets from the SharePoint PowerShell add-on.
Install assemblies:
Download and install ther latest version of the SharePoint Server 2013 Client Components SDK, this can be downloaded from here: http://www.microsoft.com/en-us/download/details.aspx?id=35585
After the SDK and the CSOM assembly DLL’s are in place, make sure you load the assemblies before calling them.

Load assemblies

 Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
 Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

This will open up for usage of CSOM in PowerShell.

Load a context

$SPOUser = "administrator@blksthl.onmicrosoft.com"
# Uses a hardcoded password, use only during test/lab:
$SPOPassword = convertto-securestring "Password01" -asplaintext -force
# Better: $SPOPassword = Read-Host -Prompt "Please enter your password" -AsSecureString
$SPOODfBUrl = "https://blksthl.sharepoint.com/personal/jeffrey_lebowski_blksthl_com"
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SPOODfBUrl)
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($SPOUser,$SPOPassword)
$Context.RequestTimeout = 16384000
$Context.Credentials = $Credentials
$Context.ExecuteQuery()

Returns: $Context

Web

(Using $Context from the section on Context above)

$Web = $Context.Web
$Context.Load($Web)
$Context.ExecuteQuery()

Returns: $Web

List/Library

$SPODocLibName = "Documents"
$SPOList = $Web.Lists.GetByTitle($SPODocLibName)
$Context.Load($SPOList.RootFolder)
$Context.ExecuteQuery()

Returns: $SPOList

GetFileByServerRelativeUrl and GetForlderByServerRelativeUrl

In order to use the ‘Get…ByServerRelativeUrl’ methods you have to supply a relative path to the file or folder, this means a path starting from the FQDN.

Example 1
https://company.sharepoint.com/get/fileorfolder/by/relative/url
FQDN: https://company.sharepoint.com
ServerRelativeUrl: /get/fileorfolder/by/relative/url

Example 2
https://company-my.sharepoint.com/personal/firstname_lastname_company_com
FQDN: https://company-my.sharepoint.com
ServerRelativeUrl: /personal/firstname_lastname_company_com

Example file:

"/personal/jeffrey_lebowski_blksthl_com/documents/report1.xlsx"

Example folder:

 "/personal/jeffrey_lebowski_blksthl_com/documents/subfolder"

Create a file from a local copy

This can be accomplished in several ways, this is one:

1.
$LocalFile = Get-ChildItem -path "C:\Homedirs\jeff\report1.xlsx"
$FolderRelativeUrl = $SPOList.RootFolder.ServerRelativeUrl
$FileName = $LocalFile.Name
$FileUrl = $FolderRelativeUrl + "/" + $FileName
[Microsoft.SharePoint.Client.File]::SaveBinaryDirect($Web.Context, $fileUrl, $LocalFile.OpenRead(), $true)

Returns: New file created in SPO/ODfB

Create a folder from a local copy

$SPOFolder = $SPOList.RootFolder
$LocalFolder = Get-ChildItem -path "C:\Homedirs\jeff\" -Recurse -Include "folder1" 
$FolderName = $LocalFolder.Name
$NewFolder = $SPOFolder.Folders.Add($FolderName)
$Web.Context.Load($NewFolder)
$Web.Context.ExecuteQuery()

Returns: New folder created in SPO/ODfB

Set properties on a file

Input: $FileRelativeUrl, $SPOItemModifier, $SPOItemOwner, $ItemCreated, $ItemModified

$CurrentFile = $Context.web.GetFileByServerRelativeUrl($FileRelativeUrl)
$Context.Load($CurrentFile)
$Context.ExecuteQuery()
$ListItem = $CurrentFile.ListItemAllFields;
$ListItem["Editor"] = $SPOItemModifier; # Get object from ResolveUser
$Listitem["Author"] = $SPOItemOwner; # Get object from ResolveUser
$Listitem["Created"] = $ItemCreated;
$Listitem["Modified"] = $ItemModified;
$ListItem.Update()
$Context.Load($CurrentFile)
$Context.ExecuteQuery()

Returns: Folder stamped with new properties in SPO/ODfB

Set properties on a folder

Input: $FolderRelativeUrl, , $SPOItemModifier, $SPOItemOwner, $ItemCreated, $ItemModified

$CurrentFolder = $Context.web.GetFolderByServerRelativeUrl($FolderRelativeURL)
$Context.Load($CurrentFolder)
$Context.ExecuteQuery()
$SPOFolderItem = $CurrentFolder.ListItemAllFields;
$SPOItemOwner = ResolveUser $UserEmail # For ResolveUser see separate function described later in this post
$SPOFolderItem["Editor"] = $SPOItemModifier # Must be a userobject, see 'ResolveUser'
$SPOFolderItem["Author"] = $SPOItemOwner # Must be a userobject, see 'ResolveUser'
$SPOFolderItem["Created"] = $ItemCreated # In the format: "8/10/2013 7:04 PM", see 'GetItemProperties'
$SPOFolderItem["Modified"] = $ItemModified # In the format: "8/10/2013 7:04 PM", see 'GetItemProperties'
$SPOFolderItem.Update()
$Context.Load($CurrentFolder)
$Context.ExecuteQuery()

Returns: Folder stamped with new properties in SPO/ODfB

ResolveUser (Function)

Function ResolveUser ($InputUPN)
# Resolves a user to a userobject
{
    $OutputUserObject = $Web.Context.web.EnsureUser($InputUPN)
    $Web.Context.Load($OutputUserObject)
    $Web.Context.ExecuteQuery()
    Return $OutputUserObject
}

Returns: UserObject for $InputUPN (UserPrincipalName/Email)

GetItemProperties (Function)

Function GetItemProperties ($InFileObject)
# Gets basic properties to set on files and folders
{
    $Global:ItemCreated = $InFile.CreationTime
    $Global:ItemModified = $InFile.LastWriteTime
}

Returns: Global: Variables for ItemCreated and LastWriteTime of $InFileObject (File or Folder)

Thats all for now, I hope that you let me know if there is anything that seems to be wrong or does not work. The problem with describing all this in a complete way, is that it is easy to leave something out and it is also difficult to test every aspect while writing. Time is limited for all of us…
Anyway, my goal was to write a post that covered what I was myself missing…I hope that this is it. And again, please let me know if there are any mistakes in here.

References and Credits

None at this time…

Credits & many thanks to

LabCenter – you guys always publish my articles!

My family, my parents, Ia and the kids!

SP2013logo

_________________________________________________________

Enjoy!

Regards

Twitter | Technet Profile | LinkedIn

Office 365 guide series – 101 ways to share a document


 Office365logo       SP2013logo

101 ways to share a document.

Fellow SharePoint lovers! (And OneDrive for Business…)

SharedLove

Share the Love

More and more individuals and organizations are starting to realize the beauty of OneDrive for Business, the way it allows you to be always up to date and to be able to always access your information no matter where you are or on what device you are on.

This article will delve© into detail on how you can keep the information in one place, instead of spreading multiple copies and versions around like we have always done using email as the sharing method of choice (Not to mention USB sticks). As you all most likely know, every time you send an email with an attachment of one of your files, a new copy and possible a new version of that document is created, it happens out of your control as well and this is not something that we want, it has simply been the only way to share, externally for sure and internally it has been the easiest way for the lazy.

Now, what has changed? What’s new? What’s so special with OneDrive for Business so that we can share thru some kind of Microsoft magic and files never have to be sent in email? What’s up with that? Well, implementing OneDrive for Business as a part of Office 365 is one step, you can however still work like you always have…removing the old Home directory and the Shared folders is another. You can however still work like you always have, sending attachments using email, but, these steps will allow you and your coworkers to adopt a new way of doing things, a better more secure and controlled way to work.

ShareTrad1

Traditional sharing, send a copy of the original to each user, same as when printing a letter and posting it…

ShareNew1

Modern sharing, one original, no copies. Everyone reads or edits the same file.

As you all also probably know and think right now, there are other cloud services that can do this and yes, I agree, but if you have invested in Office 365 already, then you get OneDrive for Business with 1TB (!) storage for free (or it is included in the price but free sounds better, and compared to using a different service like dropbox or box, then it IS free). You have a single sign on between the different applications in Office 365 and if you have implemented ADFS, then you will even have single sign on from your PC. Yes, I know that storage will be unlimited soon…but honestly, 1TB IS unlimited…

But enough of that, now I will show you where you can share a document from your OneDrive for business.

First off, there is a setting that are configured globally in the SharePoint admin portal of Office 365 that we need to know about.

Share1

External Sharing, there are 3 levels to select from. Can be set on the tenant or per Site collection. This setting can only be configured by a Global Office 365 Administrator.
The third level means anonymous access…(No! You really shouldn’t)

Share2

(Note also that if you restrict sharing on the tenant, then you cannot allow it on the site collection level) When these are set, you can start sharing.

There are a lot of places to do this for the mobile OneDrive for Business user

Share3

– OneDrive for Business Online
– OneDrive for Business Offline (from the local cache)
– The Office Applications
– The OneDrive for Business mobile app (Windows Phone, IOS, Android)
– Office Mobile (Windows Phone, IOS, Android) only shares a link, does not grant access
– Outlook Online (formerly known as Outlook Web Access)

It is more or less the same experience everywhere, the web dialog for sharing a document looks like this, from here you can share with internal users as well as external users, and all you need is an email address.

OneDrive for Business Online

Share4

Click SHARE then select how to share, or select the document(s) and click on the Share ‘button’

Share5

The dialog then looks like this

Share6

As you can see, the checkbox for ‘Require sign-in’ is checked by default, unchecking that allows anonymous access to this document.
This checkbox is only available if anonymous sharing is enabled at the tenant and at the site collection level.

The names can be internal users by name or email address, it can be external users by email or it can be everyone.

Share7

The permission level can also be set here, they speak for themselves. (Note that sharing with edit allows the recipient to in turn share with or without edit)

Share8

You can type in a message, this will be the text in the email that is sent to the recipient

Share9

Under SHOW OPTIONS you have the option to not send an email at all.

Share10

The recipient receives an email with this content

Share11

Clicking the link takes the user straight to the shared file, in its location.

Under Shared with, you can see who currently has access to this document.

Share12

When a document is shared, you can also see that the little user icon is replaced to show that someone else besides you now also has access to this document or this file.

Share13

Unshare the file again by clicking on that icon and in the Shared with dialog, select Stop sharing and save Changes.

Share135

After a quick refresh, you will see that the little icon is back to the Padlock.

Share14

This is how you share things in the OneDrive for Business Online, it is very similar in SharePoint Online (A few exceptions like unsharing differs).

The rest you will know when you see them:

The OneDrive for Business Offline (from the local cache)

Share15

The Office Applications

Share16

The OneDrive for Business mobile app (Windows Phone, IOS, Android)

Share17

Office Mobile, Word, Excel, PowerPoint (Windows Phone, IOS, Android) only shares a link, does not grant access

Share18

Outlook Online (formerly known as Outlook Web Access)

Select INSERT

Share19

Select Share with OneDrive (They really should stop confusing the business version with the consumer version…)

Share20

Apply the proper permission level, read or read/write

Share21

Share22

Note that the file is not sent as an attachment unless you specifically choose to do so. It only looks like an attachment, the file never leaves your personal OneDrive for Business.

And, to sum it all up, a message from inside Outlook Online:

Share23

SharedLove

 

References and Credits

None at this time…

Credits & many thanks to

LabCenter – you guys always publish my articles!

Mattias Gutke at Xperta

My family.

SP2013logo

_________________________________________________________

Enjoy!

Regards

Twitter | Technet Profile | LinkedIn

The Iphone Apps of SharePoint Online and Office 365


 Office365logo       SP2013logo

Greetings SharePoint Online users!

This time I will shed some light on what Microsoft have made available for the Iphone users out there. I’m usng different phones over time, but I have an Iphone and I discovered that there is actually quite a lot you can do from and on the Iphone that is SharePoint Online related (Only focusing on the supported Microsoft realesed here)
In this post I will try to list them all and write a bit about what you can do with them.

This is the list of SharePoint Online related apps available (2014-03-23)

OneDrive  Office  Newsfeed  OneNote  Admin  OWA  RMS

1. OneDrive for Business (Formarly SkyDrive Pro)
2. Office Mobile for Office 365 Subscribers
3. SharePoint Newsfeed
4. Microsoft OneNote for Iphone
5. Office 365 Admin
6. OWA for Iphone
7. Microsoft Rights Management Sharing

Lets start to show what you can see in the App store where they are all available as free downloads, first one out is OneDrive for Business. This is not a Deep dive into technology or functionality or detailed feature sets, but rather a listing of what is available, see what can be done in the gui, the next step is your own, download the apps and try them out for yourselves.

Good luck!


1. OneDrive for Business (Formarly known as SkyDrive Pro)

OneDrive1

OneDrive15  OneDrive4  OneDrive2  OneDrive3

This is as good as they get, you can easily access your personal and shared files at any time (Located in your MySite or other organizational sites within the Office 365 tenant). OneDrive for Business will keep your OneDrive files in synch at all times and available online or offline.
OneDrive for Business or SkyDrive Pro as it was originally called, is the shit, no doubt about it.


2. Office Mobile for Office 365 Subscribers

Office for 365 1

Office for 365 2  Office for 365 3  Office for 365 4  Office for 365 5  Office for 365 6

These are the classical Office application, do some final Changes or touch ups, create the files you need while on the road without any Computer. This is a complete ‘mini Office’ compatible with Office 365.


3. SharePoint Newsfeed

SHarePoint Newsfeed 1

SHarePoint Newsfeed 2  SHarePoint Newsfeed 3  SHarePoint Newsfeed 4  SHarePoint Newsfeed 5  SHarePoint Newsfeed 6

SharePoint Newsfeed, follow what is happening in your organizations social and Corporate newsfeeds. This app will make sure that you are up to date at all times.


4. Microsoft OneNote for Iphone

OneNote 1

OneNote 6  OneNote 2  OneNote 3  OneNote 4  OneNote 5

OneNote…if you don’t use it today, start using it! It is a really really good companion in your daily work, not sure if the App can replace the real application, but you can read and edit your OneNote files in your phone, how cool is that?


5. Office 365 Admin

365 Admin

365 Admin4  365 Admin5  365 Admin6  365 Admin2  365 Admin3

For the Office 365 Administrator, or for the partners out there monitoring the Office 365 tenants for customers, this is the best app of them all(except for OneDrive for Business obviously). In the Admin ‘hub’ you can get the status of all the services in your Office 365 tenant. You see all the major services status on the start page, then you can drill down to what is failing, which Component has the issue, and you can read the log from the Microsoft maintenance staff, what they are doing to solve the issue. This is great. If onluy you could add users and reset their passwords as well…but you can’t get Everything. A good monitor/dashboard from Microsoft this anyway.


6. OWA for Iphone

OWA for Iphone

OWA for Iphone6  OWA for Iphone2  OWA for Iphone3  OWA for Iphone4  OWA for Iphone5

Well, no need to explain, this is the OWA (Outlook Web App, not Office Web Apps) experience in your Iphone. You get email, calendar and Contacts in your hand. In a familiar format.


7. Microsoft Rights Management Sharing

RMS

RMS2  RMS3  RMS4  RMS5

Microsoft Rights Management Sharing. Well…this is what it says, use the App to set IRM protection on files and to open IRM protected files. IRM protected files, and you can access them on your Iphone, who would have thought…?

Ok, thats it. These are the Apps on the Iphone that are related in one or many ways to SharePoint Online, or at least to Office 365. They are all written by Microsoft Corporation and they are free to download from the App store.

References

See Appstore in your Iphone, search for Microsoft Corporation and you will find them all.

SP2013logo

_________________________________________________________

Enjoy!

Regards

Twitter | Technet Profile | LinkedIn