PS SharePoSH:\> SPListItem / SPFile SharePoint + PowerShell User Group March 27, 2013
PS SharePoSH:\> Agenda Hello! Group Logistics (GoToMeeting/ website / register / / twitter) SPListItem / SPFile Next meeting Survey!
PS SharePoSH:\> Raymond Mitchell SharePoint Consultant – Author Dad Information Worker Kid – SharePoSHing since 2008 Hello!
PS SharePoSH:\> What about you?
PS SharePoSH:\> SPListItem List item Library item SPListItemCollection – SPList.Items
PS SharePoSH:\> SPListItem SPList.Items[5] Iterating by index can be VERY expensive
PS SharePoSH:\> SPListItem More ways to get items using SPList members – GetItems String[]items only with specific fields SPQueryitems by query SPViewitems by view SPQuery, Stringby query and view – GetItemsWithUniquePermissions
PS SharePoSH:\> SPListItem More ways to get items using SPList members – GetItemById & GetItemByIdAllFields – GetItemByIdSelectedFields – GetItemByUniqueId
PS SharePoSH:\> SPListItem Once you have an item: – SPList.Fields or SPListItem.Fields
PS SharePoSH:\> SPListItem Reading field values – Item[guid]Field’s id – Item[Int32]Field’s index – Item[String]Title or Internal Name But it isn’t always quite that easy...
PS SharePoSH:\> SPListItem Reading field values – SPFieldUser / SPFieldUserValue – SPFieldLookup / SPFieldLookupValue LookupId LookupValue $fieldName = "Assigned To" $f = $list.Fields[$fieldName] $fv = $f.GetFieldValue($item[$fieldName])
PS SharePoSH:\> SPListItem Similar for setting values $new = $list.Items.Add() $fv = new-object Microsoft.SharePoint.SPFieldUrlValue() $new["Search URL"] = $fv $new.Update()
PS SharePoSH:\> SPListItem Requires Update to commit – Update() – UpdateOverwriteVersion() – SystemUpdate(boolean) true = increment item version false = don’t update Modified or Modified By
PS SharePoSH:\> SPListItem Common Properties – Attachments – Fields – File / Folder – HasUniqueRoleAssignments Common Methods – BreakRoleInheritance – ResetRoleInheritance
PS SharePoSH:\> SPFile SPListItem.File SPFolder.Files – RootFolder property – SPWeb.GetFolder(string)URL of the folder SPWeb.GetFile(string) URL of the file
PS SharePoSH:\> SPFile Common Properties – Exists – ItemFile’s parent SPListItem – LengthFile size (in bytes) – Url
PS SharePoSH:\> SPFile Common Methods – CheckIn/CheckOut/Publish – CopyTo(string, bool) Destination and Overwrite – MoveTo – OpenBinary/OpenBinaryStream – SaveBinary/SaveBinaryStream
PS SharePoSH:\> SPFile Download files from SharePoint via SPFile (C#) $b = $file.OpenBinary() $fs = New-Object System.IO.FileStream("c:\temp\test.docx", "create", "readwrite") $bw = New-Object System.IO.BinaryWriter($fs) $bw.Write($b) $bw.Close() Just want a string? – SPWeb.GetFileAsString(string)URL
PS SharePoSH:\> Next Meeting TBD – Your Demos
PS SharePoSH:\> Resources PowerShell + SharePoint forum at PowerShell.org: – PowerShell Summit: April
PS SharePoSH:\> Resources Save files from SharePoint document library to File System sharepoint-document-library-to-file-system.aspx sharepoint-document-library-to-file-system.aspx SPListItem us/library/microsoft.sharepoint.splistitem(v=office.14).aspx us/library/microsoft.sharepoint.splistitem(v=office.14).aspx SPFile
PS SharePoSH:\> Survey!