Thursday, April 23, 2015

Check current user exists in a group using an AJAX call

$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/getByName('Site Members')/Users?         $filter=Id eq " + _spPageContextInfo.userId,
    type: "GET",
    cache: true,
    async: false,
    headers:{
        "ACCEPT": "application/json;odata=verbose"
    },
    success: function (data) {
  if (data.d.results[0] != undefined) {
        //user is a member
}
    },
    error: function () {
    }
});

Check current user exists on a group and hiding links using client object model

var userGroups;
function CheckPermissions(){
    var clientContext = new SP.ClientContext.get_current();
    var currentUser = clientContext.get_web().get_currentUser();
    clientContext.load(currentUser);
    userGroups = currentUser.get_groups();
    clientContext.load(userGroups);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onCheckPermissionSucceeded),
       Function.createDelegate(this, this.onCheckPermissionFailed));
}
function onCheckPermissionSucceeded(sender, args) {
    var groupsEnumerator = userGroups.getEnumerator();
    while (groupsEnumerator.moveNext()) {
  var group= groupsEnumerator.get_current();
  if(group.get_title() == "Site Members") {
  $("li a:contains('Link1')").hide();
$("li a:contains('Link2')").hide();
break;
  }
}
}  
function onCheckPermissionFailed(sender, args) {
  console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

Tuesday, March 3, 2015

Bypass document properties while uploading the document

var urlReferrer=document.referrer.toString ();
if(urlReferrer.indexOf("Upload")!=-1)
{
    window.frameElement.commitPopup();
}

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...