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 have a requirement, where I need to upload a document like invoice report to a record library programatic. I have used the same code as uploading document to a normal document library. Now the requirement is when I am uploading a same document again, I am getting the error as

The file is checked out for editing by SHAREPOINT\system.

Whereas when I am uploading existing document to a record library from the Sharepoint UI. It automatically renames the record(infact adds some random string at the end of the orginal file name). Is there a way, where I can achieve without writing lot of code? Looking for any sort of help.

share|improve this question
    
What have you tried? What's the code? –  RJ Cuthbertson Aug 9 '12 at 13:53
    
I have tried, but that was for normal document library –  suryakiran Aug 10 '12 at 10:21
add comment

2 Answers 2

up vote 2 down vote accepted

At last after so much of research, I have found a solution. Create an intermediate Document library and upload document. Route the document to the record center. Check out the sample code.

string strFilename="Name of the File uploaded";
Hashtable metadata=new Hashtable("Other Meta data columns");
bool IsOverwrite="True or False to overwrite the existing file";
byte[] by_FileStream="Array bytes of the uploaded file";
bool MoveToRecordCeter="True or False To move to record center"; 
string strSPSite="<Site URL>";
string strSPRecLib="<Intermediate Doc Lib>";
string RecordCenterUri="<Record Center URL>";
using (SPSite oSite = new SPSite(strSPSite))
{
    SPWeb oWeb = oSite.OpenWeb();
    string strAdditionalInformation = string.Empty;
    try
    {
        SPList oList = oWeb.Lists.TryGetList(strSPRecLib);
        if (oList != null)
        {
            SPFolder oFolder = oList.RootFolder;
            SPFile oFile = null;
            oFile = oFolder.Files.Add(oFolder.Url + "/" + strFilename, by_FileStream, metadata, IsOverwrite);
            oFile.Update();
            if (MoveToRecordCeter && RecordCenterUri.Length > 0)
            {
                string Offuri = RecordCenterUri.Trim();
                SPOfficialFileHost ofh = new SPOfficialFileHost();
                ofh.Action = SPOfficialFileAction.Move;
                ofh.OfficialFileName = oFile.Name;
                ofh.OfficialFileUrl = new Uri(Offuri);
                OfficialFileResult returnValue = oFile.SendToOfficialFile("Content-type", ofh, "", SPOfficialFileSubmissionMode.ExpirationPolicy, out strAdditionalInformation);
                if (returnValue != OfficialFileResult.Success)
                {
                    throw new Exception("Error sending file to records center:" + strAdditionalInformation);
                }
            }
            return true;
        }
        else
        {
            throw new Exception("List is not available.");
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        if (oWeb != null)
            oWeb.Dispose();
    }
}

Hope this would help some one :)

share|improve this answer
    
BTW, There should be an additional note. I user who is moving the document to a record center(programatically) must be a member of Record Center Web Service Submitters. This is must :) –  suryakiran Aug 14 '12 at 13:25
add comment

suryakiran, you need to undeclare the existing record before overwrite the file.

SPList recordList = recordWeb.Lists.TryGetList(recordLibName);

SPFile recordFile = recordWeb.GetFile(recordFileUrl);
if (recordFile.Exists && Records.IsRecord(recordFile.Item))
{
    Records.UndeclareItemAsRecord(recordFile.Item);
}

SPFile newRecordFile = recordList.RootFolder.Files.Add(
    sourceFile.Name, sourceFile.OpenBinary(), true);
share|improve this answer
2  
Please add details to your answer, think for yourself, would you find this answer helpful in its current form? :) Code and / or links is often very helpful –  Robert Lindgren Sep 6 '13 at 6:28
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.