Thursday 30 January 2014

Adding Item in Announcement Web part Using Sequential Workflow in Sharepoint using C# to simultaneously add item in to another Announcement Web part in different Site Collection

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;

namespace Annoucements.EventsReceivers.AddAnnouncementWorkflow
{
    public sealed partial class AddAnnouncementWorkflow : SequentialWorkflowActivity
    {
        public AddAnnouncementWorkflow()
        {
            InitializeComponent();
        }

        public Guid workflowId = default(System.Guid);
        public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();

        public string Title, Body, Expires, WMICountry, WMISharedGroups;
        DateTime? dateExpires = null;

        private void codeActivity1_ExecuteCode(object sender, EventArgs e)
        {
            Title = workflowProperties.Item["Title"] != null ? workflowProperties.Item["Title"].ToString() : "";
            Body = workflowProperties.Item["Body"] != null ? workflowProperties.Item["Body"].ToString() : "";
            Expires = workflowProperties.Item["Expires"] != null ? workflowProperties.Item["Expires"].ToString() : "";
            WMICountry = workflowProperties.Item["WMICountry"] != null ? workflowProperties.Item["WMICountry"].ToString() : "";

            if (WMICountry != "")
            {
                SPFieldLookupValueCollection countries = new SPFieldLookupValueCollection(WMICountry);

                //loop all new collection (if new country is added)
                foreach (SPFieldLookupValue country in countries)
                {
                    //get country details
                    SPListItem countryItem = workflowProperties.Web.Lists["Country Sites"].GetItemById(country.LookupId);

                    string email = Convert.ToString(countryItem["Contact Email"]);
                    string siteURL = Convert.ToString(countryItem["Site URL"]);
                    string active = Convert.ToString(countryItem["Active?"]);

                    //insert into record here
                    if (!string.IsNullOrEmpty(siteURL) && active.Trim().ToLower() == "true")
                    {
                        // Open the site
                        SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            using (SPSite site = new SPSite(siteURL))
                            {
                                using (SPWeb web = site.OpenWeb())
                                {
                                    SPList list = web.Lists["BVHO Announcements"];

                                    SPListItem item = list.Items.Add();

                                    SPListItem Annoucementitem = workflowProperties.Item;

                                    SPAttachmentCollection objAttchments = Annoucementitem.Attachments;

                                    if (Annoucementitem.Attachments != null)
                                    {
                                        foreach (string fileName in objAttchments)
                                        {
                                            // Perform action on the extracted attachment
                                            SPFile file = Annoucementitem.ParentList.ParentWeb.GetFile(Annoucementitem.Attachments.UrlPrefix +

                                                                                                  fileName);

                                            byte[] fileData = file.OpenBinary();

                                            item.Attachments.Add(fileName, fileData);
                                        }
                                    }

                                    item["Title"] = Title;
                                    item["Body"] = Body;
                                    item["PublisherItemID"] = workflowProperties.ItemId;

                                    if (Expires != "")
                                    {
                                        item["Expires"] = Expires;
                                    }
                                    else
                                    {
                                        item["Expires"] = dateExpires;
                                    }

                                    item.Update();
                                }
                            }
                        });
                    }
                }
            }
        }
    }
}

No comments:

Post a Comment