Thursday 30 January 2014

Adding Links in Application links list Web part Using Sequential Workflow in Sharepoint using C# to simultaneously add link in to another Application 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 Application_Links.AddApplicationLinksWorkflow
{
    public sealed partial class AddApplicationLinksWorkflow : SequentialWorkflowActivity
    {
        public AddApplicationLinksWorkflow()
        {
            InitializeComponent();
        }

        public Guid workflowId = default(System.Guid);
        public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();
        public string WMIApplicationURL, WMIApplicationName, WMICountry, WMIApplicationIconURL;
        private void codeActivity1_ExecuteCode(object sender, EventArgs e)
        {
            try
            {
                WMICountry = workflowProperties.Item["WMICountry"] != null ? workflowProperties.Item["WMICountry"].ToString() : "";
                WMIApplicationName = workflowProperties.Item["WMIApplication"] != null ? workflowProperties.Item["WMIApplication"].ToString() : "";

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

                    SPFieldLookupValue countryLinks = new SPFieldLookupValue(WMIApplicationName);

                    //loop all new collection (if new country is added)
                    foreach (SPFieldLookupValue country in countries)
                    {
                        //get application link details
                        SPListItem appItem = workflowProperties.Web.Lists["Country Application Links"].GetItemById(countryLinks.LookupId);
                        WMIApplicationIconURL = Convert.ToString(appItem["WMI Application Icon URL"]);
                        WMIApplicationURL = Convert.ToString(appItem["WMI Application URL"]);

                        //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())
                                    {
                                        SPListItemCollection listItems = web.Lists["BVHO Application Links"].Items;

                                        SPListItem Applicationlistitem = listItems.Add();

                                        string newIconURL = string.Empty;
                                        newIconURL = siteURL.Trim('/');
                                        newIconURL = newIconURL.Replace(workflowProperties.Site.Url, string.Empty);
                                        newIconURL = newIconURL + WMIApplicationIconURL;
                                        newIconURL = newIconURL.Replace("Icons/", "Icons1/");

                                        Applicationlistitem["RoutingEnabled"] = workflowProperties.Item["RoutingEnabled"];
                                        Applicationlistitem["WMI Application"] = countryLinks.LookupValue;
                                        Applicationlistitem["WMI Application URL"] = WMIApplicationURL;
                                        Applicationlistitem["WMI Application Icon URL"] = newIconURL;
                                        Applicationlistitem["PublisherItemID"] = workflowProperties.ItemId;

                                        Applicationlistitem.Update();
                                    }
                                }
                            });

                            //
                            InsertPicture(siteURL, WMIApplicationIconURL, workflowProperties);
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                InsertError(string.Format(@"https://wmiregtpportal.mcfcloud.com/country/CentroAmerica"), "Application Error Message " + workflowProperties.ItemId, Ex.Message + "~" + Ex.StackTrace, "");
            }
        }

        public void InsertPicture(string spSiteURL, string relativeURL, SPWorkflowActivationProperties properties)
        {

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(properties.List.ParentWeb.Site.ID))
                {
                    using (SPWeb web = site.OpenWeb())
                    {

                        try
                        {
                            SPList list = web.Lists["Application Icons"];
                            SPFileCollection attachcol = list.RootFolder.Files;

                            foreach (SPFile file in attachcol)
                            {
                                if (file.ServerRelativeUrl == relativeURL)
                                {
                                    SPSecurity.RunWithElevatedPrivileges(delegate()
                                    {
                                        using (SPSite siteC = new SPSite(spSiteURL))
                                        {
                                            using (SPWeb webC = siteC.OpenWeb())
                                            {
                                                bool HasAllowUnsafeUpdates = webC.AllowUnsafeUpdates;

                                                if (!HasAllowUnsafeUpdates)
                                                    webC.AllowUnsafeUpdates = true;

                                                try
                                                {
                                                    SPList listC = webC.Lists["Application Icons"];
                                                    SPListItem newItem = listC.Items.Add();
                                                    SPFileCollection oldFiles = listC.RootFolder.Files;
                                                    bool isExists = false;

                                                    foreach (SPFile oldFile in oldFiles)
                                                    {
                                                        if (oldFile.Name == file.Name)
                                                        {
                                                            isExists = true;
                                                            break;
                                                        }
                                                    }

                                                    if (!isExists)
                                                    {
                                                        listC.RootFolder.Files.Add(file.Name, file.OpenBinary());
                                                    }
                                                }
                                                catch (Exception ex)
                                                {
                                                    throw ex;
                                                }
                                                finally
                                                {
                                                    if (!HasAllowUnsafeUpdates)
                                                        webC.AllowUnsafeUpdates = false;
                                                }
                                            }
                                        }
                                    });

                                    break;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                }
            });
        }


        public void InsertError(string spSiteURL, string title, string body, string Expires)
        {
            DateTime? dateExpires = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(spSiteURL))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists["Errors"];

                        SPListItem item = list.Items.Add();

                        item["Title"] = title;
                        item["Body"] = body;

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

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

    }
}

No comments:

Post a Comment