1

I have a SharePoint Event Receiver for a feature that I am writing. On the FeatureActivated method, I want to add a control to the SharePoint Master Pages. In order to do this, I have elected to inject some text into the master page to add the control by opening the the file with a StreamReader, reading the stream to text, inserting the text, then saving the file back with a SharePoint Object model.

I'm having an issue with the StreamReader. When it reads the stream to text, all I get back are a bunch of question marks.

Here is the code:

foreach(SPFile file in files)
{
    switch(new FileInfo(file.Name).Extension)
    {
        case ".master":
            string masterString = string.Empty;
            using(StreamReader reader = new StreamReader(file.OpenBinaryStream()))
            {
                masterString = reader.ReadToEnd();
            }
            //
            // Inject the text I want into the masterString
            // Convert masterString into byte []
            // Save the byte [] back to SharePoint, overwriting the master 
            // page                          
            //
            break;
    }
}

The masterString always comes back as ???????????????????????.... I need it to come back as plain text.

Any ideas?

1 Answers1

0

Experiment with different encodings (UTF-8, UTF-16, etc.)

public StreamReader(
string path,
Encoding encoding)
keifer94
  • 136
  • 2
  • Thanks for the response. I tried the following with no luck: Encoding.UTF32 Encoding.UTF7 Encoding.UTF8 Encoding.Unicode Encoding.Default – Darrell Lloyd Harvey Jan 15 '13 at 09:40
  • The stream is probably returning a byte array instead of text that can be put in a string. Experiment with the [Convert.ToBase64String](http://msdn.microsoft.com/en-us/library/dhx0d524.aspx) method. Additionally, you may need to use a BinaryReader instead so that the stream returns a byte array. The Master Page is probably not plain text so can't be read in the way you would like. – keifer94 Jan 16 '13 at 06:20
  • Here's another link for converting byte arrays to strings: http://stackoverflow.com/questions/11654562/how-convert-byte-array-to-string. Good Luck! – keifer94 Jan 16 '13 at 06:22