Friday, February 20, 2015

Getting data from a webservice using an AJAX call

  $.ajax({
            url: SERVICEURL,
            type: "GET",
            headers: { "accept": "application/json;odata=verbose" },
            async: false,
            crossDomain: true,
            success: function (data) {
                var email = data.getElementsByTagName("person")[0].getElementsByTagName("email")[0]; //email is the field returned by the service
                if (email != null && email != undefined)
                    document.getElementById('lblEmail').innerHTML = email[0].childNodes[0].nodeValue
            },
            error: function (xhr) {
                console.log(xhr.status + ': ' + xhr.statusText);
            }
        });

Getting data from a webservice

var xmlhttp = new XMLHttpRequest();
        if (window.XMLHttpRequest)
            xmlhttp = new XMLHttpRequest();
        else
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        xmlhttp.open("GET", SERVICEURL, false);
        xmlhttp.send();
        var xmlDoc = xmlhttp.responseXML;
        if (xmlDoc != null) {
                var email = xmlDoc.getElementsByTagName("email");//Here email is the field returned from the service
                if (email != null && email != undefined && email.length != 0) {
                    document.getElementById('<%= lblEmail.ClientID %>').innerHTML = email[0].childNodes[0].nodeValue;
                    document.getElementById('lnkEmail').setAttribute('href', "mailto:" + email[0].childNodes[0].nodeValue);
                }
            }
        }

Friday, February 13, 2015

Update list item with multiple attachment using server object model

SPList oList = oWeb.Lists.TryGetList(LISTNAME);
SPListItem oItem = oList.Items.Add();
oItem["Title"] = "Sample text";
oItem.Update();

HttpFileCollection hfc = HttpContext.Current.Request.Files;
for (int iCount = 0; iCount < hfc.Count; iCount++)
{
    {
        HttpPostedFile hpf = hfc[iCount];
        if (!String.IsNullOrEmpty(hpf.FileName))
        {
            Stream fStream = hpf.InputStream;
            byte[] contents = new byte[fStream.Length];
            fStream.Read(contents, 0, (int)fStream.Length);
            fStream.Close();
            fStream.Dispose();

            SPAttachmentCollection attachments = oItem.Attachments;
            string fileName = Path.GetFileName(hpf.FileName);
            attachments.Add(fileName, contents);
        }
    }
}
oItem.Update();

Thursday, February 5, 2015

Updating list item with attachment using CSOM

ClientContext clientContext = new ClientContext(siteUrl);
List oList = clientContext.Web.Lists.GetByTitle(listName);
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem oListItem = oList.AddItem(itemCreateInfo);
oListItem["Title"] = "Sample Text";
oListItem.Update();
clientContext.ExecuteQuery();
if (FileUploadControl.HasFiles)
{
foreach (var file in FileUploadControl.PostedFiles)
        {
        byte[] contents = new byte[Convert.ToInt32(file.ContentLength)];
                Stream fStream = file.InputStream;
                fStream.Read(contents, 0, Convert.ToInt32(file.ContentLength));
                fStream.Close();
                MemoryStream mStream = new MemoryStream(contents);
                AttachmentCreationInformation aci = new AttachmentCreationInformation();
                aci.ContentStream = mStream;
                aci.FileName = file.FileName;
                Attachment attachment = oListItem.AttachmentFiles.Add(aci);
                clientContext.Load(attachment);
                clientContext.ExecuteQuery();
}
        FileUploadControl.Dispose();
}
clientContext.Dispose();

How to launch a document by clicking a link to the file?

Word: <a href='ms-word:ofe|u|path/to/web/word/document.docx'>Link to document</a> Excel: <a href='ms-excel:o...