Checking if a site already exists

  • 27 January 2011
  • 2 replies
  • 0 views

Badge +6

Hi,


In an employment process we are developing, their is a requirement that every hired employee gets a sub site in the HR site, where all their documents are stored.


I have run into a problem now with matching the actual process to the way business is conducted, because they can "hire" an existing employee, IE: get a promotion or relocation within the company. If this happens, I want to check if the employee they selected has a site in HR already, we are using the employee's ID numbers as the site urls. How can I go about checking to see if the selected hire's ID number already exists in HR as a site ?


2 replies

Badge +8

I have the same requirement, did you find a solution for this?

Badge +8

Here's what I ended up doing. I added the Sharepoint webservice to the project and referenced it in the code behind. I then gave it a starting url and returned an array of existing folders . I then checked the array for the existence of the value I was looking for (in my care 'Carrier') and then if Carrier existed i looked for State. I would set a flag at each level and exit the task to create new folders at whichever level I needed to start creating folders.


 using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using SourceCode.KO;
using SourceCode.Workflow.Common.Extenders;
using SourceCode.SharePoint.WebServices;
using vmlgmtdvin01_SiteData;
using hostContext = Project_fe986877f2d3452fa0a1b71da8cefc8f.EventItemContext_4d1538dcaa414c898967799daba0e73e;
namespace ExtenderProject_fe986877f2d3452fa0a1b71da8cefc8f
{
    public partial class EventItem_4d1538dcaa414c898967799daba0e73e : ICodeExtender<hostContext>
 {
        public void Main(Project_fe986877f2d3452fa0a1b71da8cefc8f.EventItemContext_4d1538dcaa414c898967799daba0e73e K2)
        {
            string _carrier = K2.ProcessInstance.DataFields["CarrierNameAsDocLibrary"].Value.ToString();
            string _year = K2.ProcessInstance.DataFields["Year"].Value.ToString();
            string _state = K2.ProcessInstance.DataFields["State"].Value.ToString();
            string _county = K2.ProcessInstance.DataFields["County"].Value.ToString();
            string _enumerableLibrary = K2.StringTable["NCREnumerableLibrary"];
            string _url = "";
            _sFPUrl[] carrierArray;
            _sFPUrl[] stateArray;
            _sFPUrl[] yearArray;
            _sFPUrl[] countyArray;
            K2.ProcessInstance.DataFields["CarrierFlag"].Value = false;
            K2.ProcessInstance.DataFields["StateFlag"].Value = false;
            K2.ProcessInstance.DataFields["YearFlag"].Value = false;
            K2.ProcessInstance.DataFields["CountyFlag"].Value = false;


            _url = K2.StringTable["SharePointServer"];
            _url += K2.StringTable["NCRPathToDocLibrary"];
            _url += "/_vti_bin/SiteData.asmx";
            try
            {
                carrierArray = GetUrls(_url, _enumerableLibrary);
                K2.ProcessInstance.DataFields["CarrierFlag"].Value = DoesArrayContain(carrierArray, _carrier);
            }
            catch (Exception ex)
            {
                throw new Exception("carrierArray Failed" + ex.Message + ex.Source + ex.StackTrace);
            }
            if ((bool)K2.ProcessInstance.DataFields["CarrierFlag"].Value == true)
            {
                try
                {
                    stateArray = GetUrls(_url, _enumerableLibrary + "/" + _carrier);
                    K2.ProcessInstance.DataFields["StateFlag"].Value = DoesArrayContain(stateArray, "/" + _state);
                }
                catch (Exception ex)
                {
                    throw new Exception("stateArray Failed" + ex.Message + ex.Source + ex.StackTrace);
                }
            }


            if ((bool)K2.ProcessInstance.DataFields["StateFlag"].Value == true)
            {
                try
                {
                    yearArray = GetUrls(_url, _enumerableLibrary + "/" + _carrier + "/" + _state);
                    K2.ProcessInstance.DataFields["YearFlag"].Value = DoesArrayContain(yearArray, "/" + _year);
                }
                catch (Exception ex)
                {
                    throw new Exception("yearArray Failed" + ex.Message + ex.Source + ex.StackTrace);
                }
            }
            if ((bool)K2.ProcessInstance.DataFields["YearFlag"].Value == true)
            {
                try
                {
                    countyArray = GetUrls(_url, _enumerableLibrary + "/" + _carrier + "/" + _state + "/" + _year);
                    K2.ProcessInstance.DataFields["CountyFlag"].Value = DoesArrayContain(countyArray, "/" + _year);
                }
                catch (Exception ex)
                {
                    throw new Exception("countyArray Failed" + ex.Message + ex.Source + ex.StackTrace);
                }
            }
            //throw new Exception("Stop Here");             
        }


        private _sFPUrl[] GetUrls(string webServiceURL, string docLibraryFolderPathToEnumerate)
        {
            SiteData srvSiteData = new SiteData();
            srvSiteData.Credentials = System.Net.CredentialCache.DefaultCredentials;
            srvSiteData.Url = webServiceURL;


            _sFPUrl[] enArray;


            try
            {
                srvSiteData.EnumerateFolder(docLibraryFolderPathToEnumerate, out enArray);
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("K2", "Exception trying to enumnerate" + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine + ex.Source);
                throw ex;
            }
            return enArray;
        }


        private bool DoesArrayContain(_sFPUrl[] URLArray, string ContainingText)
        {
            bool _flag = false;
            if (URLArray.Length > 0)
            {
                foreach (_sFPUrl _URL in URLArray)
                {
                    if (_URL.IsFolder)
                    {
                        if (_URL.Url.ToString().Contains(ContainingText))
                        {
                            _flag = true;
                        }
                    }
                }
            }
            return _flag;
        }
    }
}



 



 I hope this may help someone else in the future.


 


 

Reply