Showing posts with label Helper functions. Show all posts
Showing posts with label Helper functions. Show all posts

Friday, June 8, 2012

Data Migration to the cloud!

Data Migration from a third party application to Sharepoint is often a requirement and I had this challenge on the Sharepoint online environment, where we do not have access to the Central Admin or the server itself.

Where there is a will there is a way! We had a challenging situation and we figured out a workaround to do the needful...

Since it was Lotus application data- we needed a means to get the data in a Microsoft format so that we could read it ( any third party connections are not supported in the cloud yet), Lotus connectors are not supported.

Solution : get all the data ported to an excel format which can be read from Sharepoint environment!
The data includes attachments as well - so design your excel in such a manner that the last 2 columns have the below information:


  1. Number of attachments
  2. Path and Name of attachments in the format ( c://attachments/recordId/test1.docx)

This uses the sharepoint client object model to upload the data and since the attachments behaves a little sneaky I used the web services to upload the attachments once the record was created!
There are enough comments in the code to explain the logic but in case you have any questions please let me know!

This console application needs to run in the same domain for picking up Admin credentials of the logged ion user for the upload process and takes a config file which details the parameters like, site, list name etc. Add a web Reference to the
http://localhost/_vti_bin/Lists.asmx and call it "SharepointListWS"

Here is the sample excel format : The second hidden row of the excel is the sharepoint column names from the List where the data is uploaded to.

Here is the code snippet that I created to import data from Lotus apps ( in a Excel format) to SPO world.

   
  
  
  private static void importFromExcel(StreamWriter streamwriterW)
  {
  
  try
  
 {
  
 Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
  
 string workbookPath = _ExcelPath;
  
 Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, false, 5,
  
 "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true);
  
 Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;
  
 Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item("Sheet1");
  
 object[,] values = excelSheet.get_Range(_ExcelRange).Value as object[,];
  
 ClientContext context = new ClientContext(_SharePointSiteUrl);
  
 context.AuthenticationMode = ClientAuthenticationMode.Default;
  
 Web web = context.Web;
  
 List list = web.Lists.GetByTitle(_ListName);
  
 for (int row = 3; row <= Convert.ToInt32(_ExcelRowNo); row++) // no. of rows of data in excel
  
 {
  
  ListItemCreationInformation listItemCreation = new ListItemCreationInformation();
  
 ListItem listItem = list.AddItem(listItemCreation);
  
 iRecord++;
  
 for (int column = 1; column <= Convert.ToInt32(_iColumnsToImport) + 1; column++) // no of fields/columns in excel to be imported
  
 {
  
 string columnName = values[2, column].ToString();
  
 strcolumnName = columnName;
  
 if (values[row, column] != null)
  
 {
  
  if (columnName == "AttachmentNo")
  
 {
  
 _strNoOfAttachments = values[row, column].ToString();
  
 strcolumnValue = _strNoOfAttachments;
  
 }
  
  else
  
 {
  
 listItem[columnName] = values[row, column];
  
 strcolumnValue = listItem[columnName].ToString(); 
  
 }
  
 }
  
 }
  
 listItem.Update();
  
 context.ExecuteQuery();
  
 string strListItemID = listItem.Id.ToString();
  
 _NoOfRowsImported++;
  
 
 logToFile("Record imported from Excel Row No :" + (iRecord + 2) + " to SP Record Id : " + strListItemID, streamwriterW);
  
 logToFile("No. of Attachments : " + _strNoOfAttachments, streamwriterW);
  
   if (_strNoOfAttachments != "0" && _strNoOfAttachments != null)
  
 {
  
 addAttachment(strListItemID, values[row, Convert.ToInt32(_iColumnsToImport) + 2].ToString(), streamwriterW);// if no. of attachment >0 
  
 }
  
 }
  
 excelWorkbook.Close();
  
 }
  
  catch (Exception e)
  
 {
  
 // log error here 
  
 }
  
 }
  
  private static void addAttachment(string strNewListItemID, string strAttachmentPathUnparsed, StreamWriter streamwriterW)
  
 {
  
  string attachmentUrl = string.Empty;
  
 try
  
 {
  
 //Parse path if delimiter exists 
  
 string[] splitAttachmentPath = strAttachmentPathUnparsed.Split(new Char[] { '|' });
  
 foreach (string strAttachmentPathParsed in splitAttachmentPath)
  
 {
  
  if (strAttachmentPathParsed.Trim() != "")
  
 {
  
 strAttachmentPath = strAttachmentPathParsed;
  
  if (!System.IO.File.Exists(strAttachmentPathParsed))
  
 {
  
 // log error

   return;
  
 }
  
  string fileName = Path.GetFileName(strAttachmentPathParsed);
  
 // Get file contents
  
 Byte[] contents = getFileStream(strAttachmentPathParsed);
  
 // Add the attachment to the new item in the SharePoint list & Instantiate a SharePoint
  
 // List Web Service object
  
 listWs = new SharePointListWS.Lists();
  
 // set the Web Service Credentials
  
 listWs.Credentials = System.Net.CredentialCache.DefaultCredentials;
  
 listWs.Url = _SharePointSiteUrl + "/_vti_bin/lists.asmx";
  
 attachmentUrl = listWs.AddAttachment(_ListName, strNewListItemID, fileName, contents);
  
 }
  
 }
  
    
 }
  
  catch (Exception ex3)
  
 {
  
 //log error  
 }
  
  if (!String.IsNullOrEmpty(attachmentUrl))
  
 {
  
     // log error

 }
  
 }
  
 }
  
 }
  

Monday, April 30, 2012

Helpers in Sharepoint Code :

Last week I managed to secure decent score in getting my MCTS Sharepoint configuration exam.
Being a hectic week ( I had a >100% utilization as well at work), I wanted to share some good code snippets with everyone which helps us get things done faster in Sharepoint ...I am panning to share with everyone my preperation guide and my strategy to get going in a week's timeframe if someone's interested in getting certified!

Now..coming back to the blog...
Helper functions to the rescue: Often as developers we use helper functions, I have planned to use this space to collate them here to get all of them at one Central location!

  • Get Custom User profile properties
Often we create custom properties in Active directory which need to be accessible via code. In my most recent project we had such a scenario and a generic helper function below helped us in code reuse

accountName can be the account of the current logged in user  or any user and property is the string which we need to find.
   // this function fetches a property of a user
        public static string GetUserProperty(SPSite site, string accountName, string property)
        {
            try
            {
                string userProperty = string.Empty;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPServiceContext context = SPServiceContext.GetContext(site);
                    var UserProfMgr = new UserProfileManager(context);
                    UserProfile UserProf = UserProfMgr.GetUserProfile(accountName);

                    if (UserProf[property].Value != null)
                    {
                        userProperty = UserProf[property].Value.ToString();
                    }
                });
                return userProperty;
            }

            catch (Exception ex)
            {
                throw new Exception("Could not get the property:" + property + "of the User:" + accountName);
            }

        }
    }
  • Trace Log an Exception
Often for the development team it is a challenge to debug issues in share point. Catching and Throwing proper exceptions is the Best way to troubleshoot issues. the one class i have started using recently is below :

    Define an enum for the severity :

   public enum LogSeverity
    {
        Unexpected,
        High,
        Medium,
        Info,
        Monitorable,
        Verbose,
        VerboseEx,
        None
    }


Class file :
 public static class TraceLog
    {
        private static DiagnosticsService myULS = null;

        public static void Log(string msg, LogSeverity severity)
        {
            if (myULS == null)
            {
                myULS = DiagnosticsService.Local;
            }

            if (myULS != null)
            {
                TraceSeverity tSeverity = mapTraceSeverity(severity);
                SPDiagnosticsCategory cat = myULS[CategoryId.General];

                myULS.WriteTrace(5555, cat, tSeverity, msg, myULS.TypeName);
            }
        }

        public enum CategoryId
        {
            None = 0,
            Deployment = 100,
            Provisioning = 200,
            CustomAction = 300,
            Rendering = 400,
            WebPart = 500,
            General = 600
        }

        [System.Runtime.InteropServices.GuidAttribute("DBEWW5AB-C5A7-46B5-A2BB-5581F960C333")]
        class DiagnosticsService : SPDiagnosticsServiceBase
        {
            private static string DiagnosticsAreaName = "Project Name";

            public DiagnosticsService()
            {
            }

            public DiagnosticsService(string name, SPFarm farm)
                : base(name, farm)
            {
            }

            protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
            {
                List<SPDiagnosticsCategory> categories = new List<SPDiagnosticsCategory>();
                foreach (string catName in Enum.GetNames(typeof(CategoryId)))
                {
                    uint catId = (uint)(int)Enum.Parse(typeof(CategoryId), catName);
                    categories.Add(new SPDiagnosticsCategory(catName, TraceSeverity.Verbose, EventSeverity.Error, 0, catId));
                }

                yield return new SPDiagnosticsArea(DiagnosticsAreaName, categories);
            }

            public static DiagnosticsService Local
            {
                get
                {
                    return SPDiagnosticsServiceBase.GetLocal<DiagnosticsService>();
                }
            }

            public SPDiagnosticsCategory this[CategoryId id]
            {
                get
                {
                    return Areas[DiagnosticsAreaName].Categories[id.ToString()];
                }
            }
        }

        private static TraceSeverity mapTraceSeverity(LogSeverity severity)
        {
            TraceSeverity tr = TraceSeverity.None;
            switch (severity)
            {
                case LogSeverity.Unexpected:
                    tr = TraceSeverity.Unexpected;
                    break;
                case LogSeverity.High:
                    tr = TraceSeverity.High;
                    break;
                case LogSeverity.Monitorable:
                    tr = TraceSeverity.Monitorable;
                    break;
                case LogSeverity.Medium:
                    tr = TraceSeverity.Medium;
                    break;
                case LogSeverity.Info:
                case LogSeverity.Verbose:
                    tr = TraceSeverity.Verbose;
                    break;
                case LogSeverity.VerboseEx:
                    tr = TraceSeverity.VerboseEx;
                    break;
                case LogSeverity.None:
                    tr = TraceSeverity.None;
                    break;
            }
            return tr;
        }
    }

A simple call in catch will, serve the purpose

catch (Exception ex)
            {
                throw new Exception("Exception Occurred, Details :" + " + ex.message.tostring());
            }