/** * Create a SharePoint Folder * * @param SPFolderInterface $folder Parent SharePoint Folder * @param array $name SharePoint Folder name * @param array $settings Instantiation settings * @throws SPRuntimeException * @return SPFolder */ public static function create(SPFolderInterface $folder, $name, array $settings = []) { if (!$folder->isWritable()) { throw new SPRuntimeException(sprintf('Folder/File operations are not allowed on a SPList Template Type [%s]', $folder->getTemplate())); } $body = json_encode(['odata.type' => 'SP.Folder', 'ServerRelativeUrl' => $folder->getRelativeUrl($name)]); $json = $folder->request('_api/web/Folders', ['headers' => ['Authorization' => 'Bearer ' . $folder->getSPAccessToken(), 'Accept' => 'application/json', 'X-RequestDigest' => $folder->getSPContextInfo()->getFormDigest(), 'Content-type' => 'application/json', 'Content-length' => strlen($body)], 'query' => ['$expand' => 'ListItemAllFields/ParentList,Properties'], 'body' => $body], 'POST'); return new static($folder->getSPSite(), $json, $settings); }
/** * Copy a SharePoint File * * @param SPFolderInterface $folder SharePoint Folder to copy to * @param string $name SharePoint File name * @param bool $overwrite Overwrite if file already exists? * @param array $extra Extra payload values to map * @throws SPRuntimeException * @return SPFile */ public function copy(SPFolderInterface $folder, $name = null, $overwrite = false, array $extra = []) { if (!$folder->isWritable()) { throw new SPRuntimeException(sprintf('Folder/File operations are not allowed on a SPList Template Type [%s]', $folder->getTemplate())); } $newUrl = $folder->getRelativeUrl($name ?: $this->name); $this->folder->request("_api/web/GetFileByServerRelativeUrl('" . $this->relativeUrl . "')/copyTo(strNewUrl='" . $newUrl . "',boverwrite=" . ($overwrite ? 'true' : 'false') . ")", ['headers' => ['Authorization' => 'Bearer ' . $folder->getSPAccessToken(), 'Accept' => 'application/json', 'X-RequestDigest' => $this->folder->getSPContextInfo()->getFormDigest()]], 'POST'); // Since the SharePoint API doesn't return a proper response on // a successful copy operation, we do a second request to get the // copied SPFile return static::getByRelativeUrl($folder->getSPSite(), $newUrl, $extra); }