Plastic tubes and pots and pans, bits and pieces…
bubbles

Plastic tubes and pots and pans, bits and pieces…

Yes, if you grew up in the 80s, you are now singing “Weird Science”. You’re welcome! So why do I want you to be singing that song? Well.. because I am, of course! Actually I think it is the perfect title for this post, and it is such a catchy tune, you just have to sing while reading. I really like the line “Fantasy and microchips, shooting from the hip.” Doesn’t that just really describe what SharePoint is and does?

So why all of the Weird Science? Well, much like Gary and Wyatt, I pieced together a bunch of stuff and made a creation of my own. No, I didn’t create Lisa, but I did create a nifty little something that some of you other admins, devs, and branders might find useful.

Large lists and libraries are becoming more and more common and the issues that come with it are going to be something that all of us will have to overcome. I recently had to troubleshoot a problem with large list throttling on a document library, and needed to see if I could recreate the issue. This sounds simple, get a document library with thousands of documents and create some views and make sure that you have an indexed column to see the difference in your views with filters. Well, I didn’t have the time or desire to create thousands of documents or to upload them, do you?

So I thought hey, there’s bound to be a way to do this with something like a PowerShell script. I began to search and found a few scripts online for uploading documents, but again, I didn’t want to deal with creating the documents to upload. I pinged my good friend and mentor Todd Klindt to see if he maybe had a script already that would do this. He had one that would create list items and shared that with me, which was very helpful in getting this started. I did use it to test the issue with lists, but still needed to work with documents in a library.

So now what? I needed to create something that would create the documents and would also add metadata in columns. I did some research and found that creating list items was a little different than creating files in a document library. I found out using SPFileCollection.Add was what must be used to create items. I found this blog post that showed how to create a new wiki page in a wiki library and modified it some to create a document in a document library.

I took part of the script Todd had given me and part of the script that the other guy had written and used it to test creating multiple documents. As it would turn out, this was the easy part. Below is a script you can use to create a bunch of documents in a library.

 

   1:  # connect to sharepoint and open the library
   2:  $library = (get-spweb http://portal.s7gear.com/IT/Help).lists["LargeLibrary"]
   3:  
   4:  “Got List…”
   5:  for ($i=1; $i -le 1000; $i++)
   6:  # try to add a new item # Try to add a new item
   7:  {
   8:  [byte[]]$mydata = “Lori Rocks, with help!”.ToCharArray(); # dummydata
   9:  $item = $library.RootFolder.Files.Add("ItemTitle" + $i + ".doc", $mydata)
  10:  }

 

This was very cool. Of course, now I had a bunch of documents, but no metadata to work with for list filtering. The $mydata string can be changed to whatever you want the document to contain. Of course, this means it really can only be used for testing, since you can’t create a huge formatted document, but at least you can create a bunch of them!

Now comes the fun part. Adding in metadata. It turns out that using the SPFileCollection.Add method allows you to add the data to the document and title the document, but adding metadata is much more difficult. It apparently includes the ability to create a hash table that can be used for this, but I was unsuccessful in my non-dev attempts to get this to work. So I thought about it and decided that it might be possible to add the metadata after the creation of the document. I didn’t have check out forced on my library, so it should be a fairly simple thing to do, right? Yeah… a dev probably could have done this in their sleep, but not me, oh no, it was a challenge, but one I was willing to accept!

So after repeatedly banging my head on my desk, and bouncing ideas off of Todd (lifesaver!!!!) I was finally able to come up with a solution that would add the metadata to existing documents.

   1:  # connect to sharepoint and open the library
   2:  $library = (get-spweb http://portal.s7gear.com/IT/Help).lists["LargeLibrary"]
   3:  "Got List"
   4:  #SetMetadataforItems
   5:  
   6:  $library.Items|Where-Object {$_.Name -like "ItemTitle1*"}|ForEach-Object{
   7:  $_["Number"]="1"; $_["Indexer"]="1"; $_.Update()}

 

This is great, right? It would find only those items that matched the title pattern and add the metadata to the appropriate columns. Which is a great little tool. Now, to put it all together:

   1:  # connect to sharepoint and open the library
   2:  $library = (get-spweb http://portal.s7gear.com/IT/Help).lists["LargeLibrary"]
   3:  
   4:  “Got List…”
   5:  
   6:  for ($i=1; $i -le 1000; $i++)
   7:  # create new document
   8:  {
   9:  [byte[]]$mydata = “Lori Rocks, with help!”.ToCharArray(); # dummydata
  10:  $item = $library.RootFolder.Files.Add("ItemTitle010-" + $i + ".doc", $mydata)
  11:  }
  12:  
  13:  #Add Metadata
  14:  $library.Items|Where-Object {$_.Name -like "ItemTitle010-*"}|ForEach-Object{
  15:  $_["Number"]="010"; $_["Indexer"]="010"; $_.Update()}

With this tool, you can create documents with different titles and metadata. You need only to change the titles and metadata for each series of documents that you want to create and run the script again. In my case, I ran the script 10 times and created 10000 items in a matter of a few minutes, versus the hours it would take to create the documents and upload them. I do recommend using a number with a dash if that is how you choose to name your documents so that you can keep each series separate. Don’t forget if your column is a lookup type of column, you’ll have to make the value of it like: “ID’;#Value”. Where the ID is the id of the value of the item.

I hope this helps those of you out there who may need to build in a large list or library for troubleshooting, branding, or performance testing and evaluation.

Oh, and big thanks to Mike Mukalian (@mmukalian) for keeping me sane during the troubleshooting part, I didn’t go into the details in this post, but he was a great help!

 

Happy SharePointing!

This post is cross-posted from here.

 

Disclaimer
The sample scripts are not supported under any Summit 7 Systems standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Summit 7 Systems further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Summit 7 Systems, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Summit 7 Systems has been advised of the possibility of such damages.

Leave a Reply