Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

What would be powershell version of this c# code? I am having hard time get this work in Powershell especially stringbuilder and files.add line where utf8encoding is. Here is my powershell version of the code. http://wikisend.com/download/429800/Add_Files_PS.txt. Here is the error.

Missing expression after ','.
At C:\PowerShell\Add_Item.ps1:60 char:73
+         $newFile = $docLibrary.RootFolder.Files.Add($newDestinationFolderPath, <<<<
UTF8Encoding.UTF8.GetBytes(builder.ToString()), $true)
    + CategoryInfo          : ParserError: (,:String) [], ParseException
    + FullyQualifiedErrorId : MissingExpressionAfterToken
using ( SPSite siteCollection = new SPSite( "http://moss.litwareinc.com" ) ) {
    using ( SPWeb web = siteCollection.OpenWeb( "docs" ) ) {
        SPList list = web.Lists["Sample"];

        //link to the file
        string fileLinkUrl = "http://moss.litwareinc.com/docs/Shared%20Documents/ConfigureIRMinWSS30.doc";

        StringBuilder builder = new StringBuilder();

        using ( TextReader reader = new StreamReader( @"C:\linktodocumenttemplate.txt" ) ) {
            builder.Append( reader.ReadToEnd() );
        }

        //replace string template with values
        builder.Replace( "{0}", fileLinkUrl );

        //should change the name of the .aspx file per item
        SPFile file = list.RootFolder.Files.Add( "link_title.aspx", UTF8Encoding.UTF8.GetBytes(builder.ToString()));

        //set list item properties
        SPListItem item = file.Item;
        item["Content Type"] = "Link to a Document";
        SPFieldUrlValue itemUrl = new SPFieldUrlValue();
        itemUrl.Description = "From sample code";
        itemUrl.Url = fileLinkUrl;
        item["URL"] = itemUrl;
        //persist changes
        item.Update();
    }
}
share|improve this question
add comment

3 Answers 3

Edit: What error message you get while running your C# code? I have tested this sample code and it works fine for me.

In any case, you can Run/test your C# code using PowerShell, which means your existing C# code could be reused inside Powershell without a need to implement it as Cmdlet.

Technet sample: http://blogs.technet.com/b/stefan_gossner/archive/2010/05/07/using-csharp-c-code-in-powershell-scripts.aspx

share|improve this answer
    
Thanks. Let me try that. ranaictiu-technicalblog.blogspot.com/2010/07/… –  Monica Lewinsky Jul 2 '12 at 20:20
    
I already converted the code into powershell but having trouble files.add line. I think i need to convert utf8 ... to ps friendly. –  Monica Lewinsky Jul 2 '12 at 20:35
    
it seems that you have copied the code from blogs.msdn.com/b/cliffgreen/archive/2008/06/23/…. Have your created a site and document library as suggested in the code, or changed your parameters? –  Falak Mahmood Jul 2 '12 at 20:36
    
Could you please post the error message? –  Falak Mahmood Jul 2 '12 at 20:37
    
I uploaded the code in a txt file. wikisend.com/download/429800/Add_Files_PS.txt –  Monica Lewinsky Jul 2 '12 at 22:19
add comment

This may work: http://blogs.msdn.com/b/sowmyancs/archive/2008/05/31/how-we-can-create-documents-inside-a-document-library-based-upon-our-content-type-s-document-template.aspx

Powershell:

$oFile = $DocLibrary.RootFolder.Files.Add("TestDoc1.aspx", oWeb.GetFile(oContentType .DocumentTemplateUrl).OpenBinary(), true);
share|improve this answer
    
I am getting error on $eb.GetFile Exception calling "GetFile" with "1" argument(s): "Value does not fall within the expected range." At C:\PowerShell\Add_Item.ps1:65 char:86 + $file = $spLibrary.RootFolder.Files.Add($filewithpath, $web.GetFile <<<< ($ct.DocumentT emplateUrl).OpenBinary(), $true) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException –  Tom Cruise Katy Holmes Jul 3 '12 at 2:11
    
I put the whole path to getfile method get one more error message in addition to the above error. What the Hell? Microsoft has NO good documentation on this. Exception calling "OpenBinary" with "0" argument(s): "Cannot open file "_layout/newlink.aspx"." At C:\PS\Add_Item.ps1:70 char:40 + $binFile = $spf.OpenBinary <<<< () + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException –  Tom Cruise Katy Holmes Jul 3 '12 at 3:49
    
This works with some caveat. I downloaded the newlink.aspx file then uploaded to a document library say "templates". then it works. But iconUrl is not same when loading files. –  Monica Lewinsky Jul 3 '12 at 17:39
add comment

The builder part you can implement using

 $content = (Get-Content 'C:\linktodocumenttemplate.txt') -replace '\{0}', $filelinkurl

The UTF8 part you can implement using

$utf8 = New-Object -Type "System.Text.UTF8Encoding"
$bytes = $utf8.GetBytes($content)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.