Question

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();
                        }
                    }
                }
            }
        }
Was it helpful?

Solution

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
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top