0

I have 2 webapplications. One of these 2 is the record center. I created a new "send to location" connection for both webapplications. These 2 connections are both referencing to the record center. You can do this in the field "Send To Url". In code I try to send a document to to this send to location. How can I select programmatically a send to connection?

The code below works, but sends the document to the DropOffLibrary inside its own site collection. I want to send it to the record center.

private void SendFileToRMA(string listID, string[] itemIdArray)
        {
            string destination = null; // New location of the file is assigned to this string.

            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists[new Guid(listID)];
                    foreach (string itemID in itemIdArray)
                    {
                        SPListItem item = list.GetItemById(Convert.ToInt32(itemID));

                        SPFile file = item.File;
                        byte[] bytes = file.OpenBinary();
                        string fileType = file.Item.ContentType.Name;
                        SPFieldCollection fields = file.Item.Fields;

                        List<RecordsRepositoryProperty> properties = new List<RecordsRepositoryProperty>();

                        // Create a RecordsRepositoryProperty for each metadata field.
                        foreach (SPField field in fields)
                        {
                            try
                            {
                                string value = Convert.ToString(
                                field.GetFieldValue(Convert.ToString(file.Item[field.Title])));

                                RecordsRepositoryProperty property =
                                new RecordsRepositoryProperty
                                {
                                    Name = field.Title,
                                    Type = field.TypeAsString,
                                    Value = value
                                };

                                properties.Add(property);
                            }
                            catch (Exception exception)
                            {
                                // log
                            }
                        }

                        OfficialFileResult outcome = OfficialFileCore.SubmitFile(web, file.OpenBinary(), properties.ToArray(), fileType, file.Web.Url + file.Url, out destination);

                        if (!outcome.Equals(OfficialFileResult.Success))
                        {
                            // log
                        }

                        // Seems that you have to manually delete the file.
                        bool originalSafeValue = web.AllowUnsafeUpdates;
                        try
                        {
                            web.AllowUnsafeUpdates = true;
                            file.Item.Delete();

                        }
                        finally
                        {
                            web.AllowUnsafeUpdates = originalSafeValue;
                            web.Update();
                        }
                    }
                }
            }
        }

1 Answer 1

0

You need to use SPOfficialFileHost:

SPOfficialFileHost fh = site.WebApplication.OfficialFileHosts.Where(y => y.OfficialFileName.Equals("MyConnectionName")).FirstOrDefault();

if (fh != null)
{
    using (SPSite rmaSite = new SPSite(fh.OfficialFileUrl.ToString()))
    {
        using (SPWeb rmaWeb = rmaSite.OpenWeb())
        {
            // do some logic
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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