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

I am trying to write some tests for an event handler. As part of it, I have a method to upload a file. When overwriting an existing file, I am always getting a NullReferenceException, despite using an overload that allows overwrites. I have tried a few different overloads, including a byte array one and verifying that the array is not null or empty. I get the same error when running with the event receiver in question uninstalled.

Any ideas why? Is there a better method to use to upload a file programatically?

    public static SPFile UploadDocument(SPList list, string path)
    {

        // Opening a filestream 
        using (var fStream = File.OpenRead(path))
        {
            string filename = Path.GetFileName(path);
            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentException("Filename portion of path cannot be null or empty", "path");
            }
            // Adding the file to the SPFileCollection 
            return list.RootFolder.Files.Add(filename, fStream, true);
        }
    }

Test method EventTesting.Rco.Overwrite threw exception: System.NullReferenceException: Object reference not set to an instance of an object.

at Microsoft.SharePoint.Library.SPRequestInternalClass.PutFile(String bstrUrl, String bstrWebRelativeUrl, Object punkFile, Int32 cbFile, Object punkFFM, PutFileOpt PutFileOpt, String bstrCreatedBy, String bstrModifiedBy, Int32 iCreatedByID, Int32 iModifiedByID, Object varTimeCreated, Object varTimeLastModified, Object varProperties, String bstrCheckinComment, Byte partitionToCheck, Int64 fragmentIdToCheck, String bstrCsvPartitionsToDelete, String bstrLockIdMatch, String bstEtagToMatch, Int32 lockType, String lockId, Int32 minutes, Int32 fRefreshLock, Int32 bValidateReqFields, Guid gNewDocId, UInt32& pdwVirusCheckStatus, String& pVirusCheckMessage, String& pEtagReturn, Byte& piLevel, Int32& pbIgnoredReqProps) at Microsoft.SharePoint.Library.SPRequest.PutFile(String bstrUrl, String bstrWebRelativeUrl, Object punkFile, Int32 cbFile, Object punkFFM, PutFileOpt PutFileOpt, String bstrCreatedBy, String bstrModifiedBy, Int32 iCreatedByID, Int32 iModifiedByID, Object varTimeCreated, Object varTimeLastModified, Object varProperties, String bstrCheckinComment, Byte partitionToCheck, Int64 fragmentIdToCheck, String bstrCsvPartitionsToDelete, String bstrLockIdMatch, String bstEtagToMatch, Int32 lockType, String lockId, Int32 minutes, Int32 fRefreshLock, Int32 bValidateReqFields, Guid gNewDocId, UInt32& pdwVirusCheckStatus, String& pVirusCheckMessage, String& pEtagReturn, Byte& piLevel, Int32& pbIgnoredReqProps) at Microsoft.SharePoint.SPFileCollection.AddStreamOrBytesInternal(String urlOfFile, Stream file, PutFileOpt fileOpt, String createdBy, String modifiedBy, Int32 createdByID, Int32 modifiedByID, DateTime timeCreated, DateTime timeLastModified, Object varProperties, String checkInComment, Stream formatMetadata, String lockIdMatch, String etagToMatch, SPLockType lockType, String lockId, TimeSpan lockTimeout, Boolean validateRequiredFields, SPVirusCheckStatus& virusCheckStatus, String& virusCheckMessage, String& etagNew, Boolean& ignoredRequiredProps) at Microsoft.SharePoint.SPFileCollection.Add(String urlOfFile, Stream file, Boolean overwrite, String checkInComment, Boolean checkRequiredFields) at Microsoft.SharePoint.SPFileCollection.Add(String urlOfFile, Stream file, Boolean overwrite) at EventTesting.TestHelpers.UploadDocument(SPList list, String path) in C:\Users\sp_setup_user\Documents\Visual Studio 2010\Projects\EventTesting\EventTesting\TestHelpers.cs:line 25 at EventTesting.Rco.Overwrite() in C:\Users\sp_setup_user\Documents\Visual Studio 2010\Projects\EventTesting\EventTesting\Rco.cs:line 151

share|improve this question
add comment

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.