Wednesday, September 2, 2015

Upload files to SharePoint using Excel

This is a minor task I undertook quite a while ago, I think someone might want to do something similar and hence decided to share the vba code, note that some of the code was sourced form somewhere else unfortunately I can't find the original source any more.


Public Sub CallWebService()
 'Set and instantiate our working objects

 Dim fileName As String
 Dim fileName2 As String
 Dim base64_data As String
 fileName = Application.GetOpenFilename("Comma Separated Values (*.pdf),*.pdf")

    If fileName <> "False" Then
        'Workbooks.Open fileName, Format:=2
         'Sheets1.Range("A2").Value = EncodeFile(fileName)
         fileName2 = Dir(fileName, vbDirectory)
         'MsgBox fileName2
         base64_data = EncodeFile(fileName)
    Else
       Return
    End If

    Dim Req As Object
    Dim sEnv As String
    Dim Resp As New MSXML2.DOMDocument60  '60
    Set Req = CreateObject("MSXML2.XMLHTTP")
    Set Resp = CreateObject("MSXML2.DOMDocument.6.0")
    Req.Open "Post", "https://home.contoso.com/_vti_bin/copy.asmx", False
    ' need to get the filename part
 
    ' we create our SOAP envelope for submission to the Web Service
 
    sEnv = ""
    sEnv = sEnv + ""
    sEnv = sEnv + ""
    sEnv = sEnv + "" + fileName + ""
    sEnv = sEnv + " "
    sEnv = sEnv + "https://home.contoso.com/sites/ITCDev/InvoiceDemo/" + fileName2 + ""
    sEnv = sEnv + "
"    sEnv = sEnv + ""
    sEnv = sEnv + ""
    sEnv = sEnv + ""
    sEnv = sEnv + " "
    sEnv = sEnv + " "
    sEnv = sEnv + " "
    sEnv = sEnv + "  "
    sEnv = sEnv + "  "
    sEnv = sEnv + "  
"    sEnv = sEnv + "   " + base64_data + ""
    sEnv = sEnv + "
"    sEnv = sEnv + "
"    sEnv = sEnv + "
"             
     'sEnv = sEnv & ";"
     'sEnv = sEnv & "  "
     'sEnv = sEnv & "  "
     'sEnv = sEnv & "   "
     'sEnv = sEnv & "    username"
     'sEnv = sEnv & "    password"
     ''Looks for SKU in active worksheet cell B3
     'sEnv = sEnv & "    " & Range("B3").Value & ""
     'sEnv = sEnv & "  
"     'sEnv = sEnv & "
"     'sEnv = sEnv & "
"    ' Send SOAP Request
 
    Req.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
    Req.setRequestHeader "SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/CopyIntoItems"
    Req.send (sEnv)

    'oXMLHttp.open("POST", strSoapURL, false, "SVC_E4SE", "Ep1c@rIC#1.2");
    'oXMLHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    'oXMLHttp.setRequestHeader("SOAPAction", "http://epicor.com/webservices/" + actionName);
    'oXMLHttp.send(oReqXML.xml);


' Display results in MessageBox
    'MsgBox Req.responseText
    Resp.LoadXML Req.responseText
    Range("B6").Value = Resp.XML
 
  'clean up code
    Set Req = Nothing
    Set Resp = Nothing
 
 
End Sub


Public Function EncodeFile(strPicPath As String) As String
    Const adTypeBinary = 1          ' Binary file is encoded

    ' Variables for encoding
    Dim objXML
    Dim objDocElem

    ' Variable for reading binary picture
    Dim objStream

    ' Open data stream from picture
    Set objStream = CreateObject("ADODB.Stream")
    objStream.Type = adTypeBinary
    objStream.Open
    objStream.LoadFromFile (strPicPath)

    ' Create XML Document object and root node
    ' that will contain the data
    Set objXML = CreateObject("MSXml2.DOMDocument")
    Set objDocElem = objXML.createElement("Base64Data")
    objDocElem.DataType = "bin.base64"

    ' Set binary value
    objDocElem.nodeTypedValue = objStream.Read()

    ' Get base64 value
    EncodeFile = objDocElem.Text

    ' Clean all
    Set objXML = Nothing
    Set objDocElem = Nothing
    Set objStream = Nothing

End Function

Friday, August 8, 2014

An interesting exercise with Powershell for implementing continuous integration with Epicor E4SE

I have been working with Epicor E4SE for a couple of years now, our systems are heavily customized and we also have an in-house development team working on development and support. As the application grows, we are building more sites and also ongoing tasks of deployment becoming quite a bit overhead, it is this point that I started to looking into ways to automate all these tasks and achieve continuous integration.
Another thing that worth mentioning, I used Jira in a previous job and found it is really handy thus introduced and implemented Jira and also its fisheye subversion integration, this helps us to manage the development team's work in a much efficient way. This does provide a foundation for us to go a step further to work towards continuous integration and work in a much more agile way. Initially I was looking into develop the build script using C# however it is not easy for us to modify the code as it goes and it is also not as easy to integrate with other scripts for system configuration which are usually written in different scripting language, the final language I chose is powershell due to its rich feature and ongoing populality, the functionalities includes the following:
> automatically check out latest version of source from subversion
> build the source using the checkout source, including components assemblies, interface assemblies, proxies interfaces and javascript web service proxies, forms, reports (aspx), a lot of these are achieved via calling Epicor.Tools
> automatically create application folders and copy application files over.
> create web application and application pool, configure web application and application pool automatically, these include E4SE web app, SecurityManager and Firesentry.

Monday, August 4, 2014

asp.net IIS won't serve default document (default.aspx) with error 404

in my case, here is the fix:
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
</handlers>
</system.webServer>

Wednesday, July 30, 2014

Automated Build CSharp code using CodeDomProvider and .csproj file

Here is the code snippet from the code I used for automated compilation of CSharp code....
 string _projectName = Path.GetFileNameWithoutExtension(projectFilePath);  
 XmlDocument _project = new XmlDocument();  
 _project.Load(projectFilePath);  
 XmlNamespaceManager ns = new XmlNamespaceManager(_project.NameTable);  
 ns.AddNamespace("msbld", "http://schemas.microsoft.com/developer/msbuild/2003");  
 XmlNode _settings = _project.SelectSingleNode("//msbld:Project/msbld:PropertyGroup/msbld:AssemblyName", ns);  
 string _asmName = _settings.InnerText;  
 XmlNodeList _files = _project.SelectNodes("//msbld:Project/msbld:ItemGroup/msbld:Compile", ns);  
 ArrayList _sourceNames = new ArrayList();  
 // Add compilable files  
 foreach (XmlNode _file in _files)  
 {  
   string _codeFilename = _file.Attributes["Include"].Value.ToLower();  
  if ( _codeFilename != "assemblyinfo.cs"      && !_codeFilename.EndsWith("vsa.cs") )       
      {  
                          // Read the source code  
           int _idx = _sourceNames.Add(_file.Attributes["Include"].Value);  
      }  
 }  
 XmlNodeList _reference = _project.SelectNodes("//msbld:Project/msbld:ItemGroup/msbld:Reference", ns);  
  foreach (XmlNode _asmRef in _reference)  
 {  
         // Try to use the AssemblyName attribute if it exists  
         string _refAsmName;  
         if (_asmRef.Attributes["AssemblyName"] != null)  
           _refAsmName = _asmRef.Attributes["AssemblyName"].Value;  
         else if (_asmRef.Attributes["Name"] != null)  
           _refAsmName = _asmRef.Attributes["Name"].Value;  
         else  
         {  
           _refAsmName = _asmRef.Attributes["Include"].Value;  
           if (_refAsmName.IndexOf(",") > 0)  
             _refAsmName = _refAsmName.Substring(0, _refAsmName.IndexOf(","));  
         }  
        cp.ReferencedAssemblies.Add(_refAsmName + ".dll");  
  }  
       cp.GenerateExecutable = false;   // result is a .DLL  
       cp.IncludeDebugInformation = true;  
       String[] _sourcefiles = (String[])_sourceNames.ToArray(typeof(string));  
       CompilerResults cr = provider.CompileAssemblyFromFile(cp, _sourcefiles);  
       if (cr.Errors.Count > 0) //if (_compErrs.Length > 0)  
                {  
                     bool _error = false;  
         foreach (CompilerError _err in cr.Errors)  
                     {                           
                          // Error or warning?  
                          if (!_err.IsWarning)//( _err.ErrorLevel != Microsoft.CSharp.ErrorLevel.Warning )  
                               _error = true;  
                          if(_error)                           
                               swrLog.WriteLine("CompileAndDeploy:CompileComponentAssemblies: Error compiling " + _projectName + ".\nPlease rectify then redeloy.");  
                          else  
                               swrLog.WriteLine("CompileAndDeploy:CompileComponentAssemblies: Warning compiling " + _projectName + ".\nPlease rectify then redeloy.");  
                          swrLog.WriteLine( _err.ErrorText);  
                     }  
                     if (_error)  
                     {  
                          swrLog.WriteLine("Compile errors occurred. Rectify first.");  
                          throw new Exception ("Compile errors occurred. Rectify first." );  
                     }  
                }  
method 2: msbuild

                           //string projectFileName = @"C:\Temp\E4SE\src\SMEC\Epicor.SMEC.IInterfaces.csproj";
                            BuildManager manager = BuildManager.DefaultBuildManager;

                            ProjectInstance projectInstance = new ProjectInstance(path);
                            var result = manager.Build(new BuildParameters()
                            {
                                DetailedSummary = true
                            },
                                new BuildRequestData(projectInstance, new string[] { "Build" }));
                            var buildResult = result.ResultsByTarget["Build"];
                            var buildResultItems = buildResult.Items;
                            mswrLog.WriteLine("Building: " + path + " Result: " + buildResult.ResultCode);

Disable Microsoft Defender for Cloud for Visual Studio Subscription (MSDN)

I use a visual studio pro subscription which comes with $150 azure cloud credit, for some reason Microsoft Defender for Cloud was turned on ...