Wednesday, April 22, 2015

Powershell to create SharePoint folder in SharePoint Library with adding two different set of files

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#Create Folder from the given Range and copy the file
# Keep the file in C:\mydoc.txt

$snapinName = "Microsoft.SharePoint.PowerShell"
if ((Get-PSSnapin | Where-Object {$_.Name -eq $snapinName }) -eq $NULL) {
  write-host "SharePoint SnapIn not loaded. Loading..."
  Add-PSSnapin $snapinName -ErrorAction SilentlyContinue
}
$webUrl = "http://sp2010:23934/"
$listName = "TEST" #Document Library
$StartCounterGUID=1000; #Starting Folder number
$EndCounterGUID = 1003; #End Folder number



 Try
       { # Start Try 
       
                # Open web and library
                $web = Get-SPWeb $webUrl
                $list = $web.Lists[$listName]
                 
                # Create desired number of subfolders
                for($i=$StartCounterGUID; $i -le $EndCounterGUID; $i++)
                {
                $folder = $list.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "$i")
                $folder.Update()

                $spFolder = $web.GetFolder("$listName\$i")
                # Get the file on Disk that we want to upload
                $file = Get-Item C:\mydoc.txt
                # upload the file.
                write-host "$listName\$i\mydoc.txt"
                [Microsoft.SharePoint.SPFile]$spFile = $spFolder.Files.Add("$i"+"mydoc.txt",$file.OpenRead(),$false)
                     
                $spFile.Item["Title"] = "$i"+"mydoc.txt"           
                $spFile.Item.Update()

                write-host "$listName\$i\yourdoc.doc"
                [Microsoft.SharePoint.SPFile]$spFile = $spFolder.Files.Add("$i"+"yourdoc.doc",$file.OpenRead(),$false)
                     
                $spFile.Item["Title"] = "$i"+"yourdoc.doc"    
                 #$spFile.Item["GUID"] = "$i"+"yourdoc.doc"          
                $spFile.Item.Update()

                write-host $i
                }
                $web.Dispose() 
              
              
    }
     
                
     catch 
   {

                 
      $web.Dispose() 
                    write-host  "Web Disposed"
                    $ErrorMessage = $_.Exception.Message
                    write-host  $ErrorMessage
                    continue
   
    }
    

No comments:

Post a Comment

HTML

Script:

JS