Thursday, December 8, 2016

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]);

}

Friday, February 17, 2012

Type was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.

While working on a GWT application incorporating a SuggestBox and a custom suggestion oracle class I got stung by the following error:

"Type 'custom suggestion class' was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialised."

The solution to my particular problem was to move the custom suggestion class into the client side package (it needs to be on the client side).


Decided to put a post up about this solution to the problem because it has a very faint web foot print, most other suggested fixes are about creating a empty constructor for your suggestion class. 

Sunday, January 15, 2012

Cannot read fields from a deleted object

The above error was causing me some grief, I did delete the object but for some reason a populate query was picking it up from the datastore.

I was able to check if the object was deleted with the following JDOHelper function: JDOHelper.isDeleted(item)

I used it as follows:


import javax.jdo.JDOHelper;
import javax.jdo.ObjectState;

public List getWatchList() {
List watchList = new ArrayList();
List result = ccs.myasxpager.datasource.WatchList.getWatchList();

for (ccs.myasxpager.persistence.schema.WatchList item : result) {
if (JDOHelper.isDeleted(item))
continue;
String[] watchListItem = {item.getAsxCode(), ccs.myasxpager.datasource.Company.getCompanyNameByCode(item.getAsxCode()), ccs.myasxpager.datasource.Company.getIndustryByCode(item.getAsxCode())};
watchList.add(watchListItem);
}
return watchList;
}


Monday, September 13, 2010

Using XmlReader to parse an XML file

using (XmlReader reader = XmlReader.Create(@"D:\Temp\XMLReader\XMLReader\ViewFields.xml"))
{
// Parse the XML document. ReadString is used to
// read the text content of the elements.
while(reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name.Equals("viewfield"))
{
reader.ReadStartElement("viewfield");
Console.WriteLine(reader.ReadString());
}
}
}
}
Console.Read();