0

I have a script where I locate a document library and try to add a folder to it, however, it fails. This is my current failing script:

$web = Get-SPWeb($completeUrl)

$DocumentLibrary = $completeUrl + "/DocumentLibrary"

$site = New-Object Microsoft.SharePoint.SPSite($DocumentLibrary)

$website = $site.OpenWeb()

$list = $website.GetList($DocumentLibrary)

$newFolder = $list.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder)
$newFolder["Title"] = "Weekly"
$newFolder.Update()

The exception that I get is: Exception calling "Update" with "0" argument(s): "Invalid item data -- missing FileRef."

Any hints?

2
  • 1
    See this answer maybe? sharepoint.stackexchange.com/questions/42016/… Commented Aug 18, 2016 at 8:44
  • is there a particular reason that you are getting to site by document library url first, then again from getLists? I think you can just directly get to document library using just this: $list = $website.GetList($DocumentLibrary) Commented Aug 18, 2016 at 8:54

2 Answers 2

1

I have just tested and this works for me:

$DocumentLibrary = "Yourlibrary's Full Url here"
$list = $web.GetList($DocumentLibrary)
$folder = $list.Items.Add("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder,"Weekly")
$folder.Update()
1
  • You are right, that seems like a smarter (and easier) way of doing it. Commented Aug 18, 2016 at 9:01
0

You cannot set an empty string as the first argument for AddItem(). Look at the reference on MSDN: https://msdn.microsoft.com/en-us/library/office/ee558947.aspx

In your case, you would have to set the server-relative url of the list as the first methode-argument. E.g.

$newFolder = $list.AddItem($website.ServerRelativeUrl + "/DocumentLibrary", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.