Friday, December 16, 2016

My Eclipse Mars Google App Engine + GWT 2.6.1 setup


To get to the screen above in Eclipse Mars: Help --> Installation Details

Had issues with the GWT designer tab not appearing.

Installed the GWT Designer GPE and the designer tab reappeared.

Thursday, December 8, 2016

SharePoint 2013 Downloading a document library using C# and Microsoft.SharePoint.Client

References

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;
using SP = Microsoft.SharePoint.Client;
using System.IO;


Main program drive code
  • Gets a list of documents from a document library
  • Loops through the list of documents
  • Checks if there is more than one version of the document
  • Loops through the list of versions in a document library and calls downloadSharePointFile to download that version.

                        ListItemCollection collListItem = querySharePointList(list.Title);
                        foreach (ListItem oListItem in collListItem)
                        {                            
                            Console.WriteLine(string.Format("{0} - {1}", oListItem["FileLeafRef"], oListItem["FileRef"]));

                            context2013.Load(oListItem);
                            context2013.Load(oListItem.File);
                            context2013.Load(oListItem.File.Versions);
                            context2013.ExecuteQuery();

                            //if (oListItem.File.Versions.Count > 1)
                                //MessageBox.Show("More than one version");

                            if (oListItem.File.Versions.Count > 1)
                            {
                                foreach (FileVersion version in oListItem.File.Versions)
                                {
                                    Console.WriteLine(String.Format("Version no: {0} url - {1}", version.VersionLabel, version.Url));
                                    downloadSharePointFile(version.Url, oListItem["FileLeafRef"].ToString());
                                }
                            }

                            //downloadSharePointFile(oListItem["FileRef"].ToString(), oListItem["FileLeafRef"].ToString());
                        }


Query the document library for a list of all the documents in the document library

title - the name of the document library

        private ListItemCollection querySharePointList(string title)
        {
            SP.List oList = context2013.Web.Lists.GetByTitle(title);
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = "5000";
            ListItemCollection collListItem = oList.GetItems(camlQuery);
            context2013.Load(collListItem);
            context2013.ExecuteQuery();
            return collListItem;
        }

Download the document from the document library

filename - url to the document
documentName - name of the document

        private void downloadSharePointFile(string filename, string documentName)
        {
            string baseUri = "base url of the sharepoint application";
            // Create a new WebClient instance.
            WebClient myWebClient = new WebClient();
            System.Net.NetworkCredential netCredential = new System.Net.NetworkCredential("username", "password", "domain name");
            myWebClient.Credentials = netCredential;
            // Concatenate the domain with the Web resource filename.
            // Download the Web resource and save it into the current filesystem folder.
            string fileUrl = baseUri  + "/" + filename;
            myWebClient.DownloadFile(fileUrl, @"c:\temp\" + documentName);
        }

Creating a new web in SharePoint 2010 using C# and Microsoft.SharePoint.Client;

References

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;
using SP = Microsoft.SharePoint.Client;

Implementation

            // Starting with ClientContext, the constructor requires a URL to the server running SharePoint.
            ClientContext context2010 = new ClientContext("SharePoint URL");

            WebCreationInformation creation = new WebCreationInformation();
            creation.Url = "New web URL name";
            creation.Title = "New web title";
            Web newWeb = context2010.Web.Webs.Add(creation);

            // Retrieve the new web information.
            context2010.Load(newWeb, w => w.Title);
            context2010.ExecuteQuery();

            MessageBox.Show("Web created");

SharePoint 2010 - Uploading a document into a document library with C# and Microsoft.SharePoint

Still working with 2010 in 2016? Me too.

You need to reference these libraries:

using Microsoft.SharePoint;
using System.IO;



        private static void uploadDocument()
        {
            String fileToUpload = @"C:\Temp\Accessibility audit of SharePoint 2010 January 2013.docx";
            String sharePointSite = "SharePoint site URL";
            String documentLibraryName = "Name of document library";
            try
            {
                using (SPSite oSite = new SPSite(sharePointSite))
                {
                    using (SPWeb oWeb = oSite.OpenWeb())
                    {
                        if (!System.IO.File.Exists(fileToUpload))
                            throw new FileNotFoundException("File not found.", fileToUpload);

                        SPFolder myLibrary = oWeb.Folders[documentLibraryName];

                        // Prepare to upload
                        Boolean replaceExistingFiles = true;
                        String fileName = System.IO.Path.GetFileName(fileToUpload);
                        FileStream fileStream = System.IO.File.OpenRead(fileToUpload);

                        // Upload document
                        SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);
                        // 2/12/2013 12:15 PM
                        spfile.Item.Properties.Clear();
                        spfile.Update();

                        // Commit
                        myLibrary.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Google Analytics Download tracking

var trackableExtensions = "zip,exe,pdf,doc,docx,rtf,xls,xlsx,ppt,pptx,mp3,jpeg,jpg";
var baseUrl = "target site url";
var relativeBasePath = "/";

$(document).ready(function () {
    $("a").on("click", function () {
        if (isTrackableExtension($(this).attr('href'))) {
            var filePath = $(this).attr('href');
            if (filePath.toLowerCase().indexOf(baseUrl) > -1)
                filePath = filePath.toLowerCase().replace(baseUrl, "");
            else
                filePath = filePath.toLowerCase().replace(relativeBasePath, "");
            sendEventToAnalytics(filePath);
        }
    });
});

function getExtensions() {
    return trackableExtensions.split(",");
}

function isTrackableExtension(linkUrl) {
    var trackable = false;
    var extenstions = getExtensions();

    for (var i = 0; i < extenstions.length; i++) {
        if (linkUrl.indexOf(extenstions[i]) > -1) {
            trackable = true;
            break;
        }
    }
    return trackable;
}

function getFileExtension(filename) {
    var extension = "";
    var extenstions = getExtensions();
    for (var i = 0; i < extenstions.length; i++) {
        if (filename.indexOf(extenstions[i]) > -1) {
            extension = extenstions[i].replace(".", "");
            break;
        }
    }
    return extension;
}

function getFileName(linkUrl) {
    var fileName = linkUrl.substring(linkUrl.lastIndexOf("/") + 1);
    return fileName;
}

function sendEventToAnalytics(filename) {
    var eventAction = 'Download-' + getFileExtension(filename).toUpperCase();
    _gaq.push(['_trackEvent', 'Document', eventAction, filename]);

}