Tuesday, June 2, 2015

SharePoint 2013 - Create an App to get lists using CSOM

'use strict';

var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser();
var web = context.get_web();
var list = web.get_lists();

(function () {
    // This code runs when the DOM is ready and creates a context object which is
    // needed to use the SharePoint object model

    $(document).ready(function () {
        getListCount();
    });

    // This function prepares, loads, and then executes a SharePoint query to get
    // the current users information

  function getListCount() {
context.load(web);
context.load(list);
        context.executeQueryAsync(onGetListCountSuccess, onGetListCountFail);
    }

    // This function is executed if the above call is successful
    // It replaces the contents of the 'message' element with the user name

    function onGetListCountSuccess() {

    var ernumerator = list.getEnumerator();
  while(ernumerator.moveNext())
  {
  var listname = ernumerator.get_current();
      alert(listname.get_title());
  }

  $('#message').text('Hello ' +list.get_count());
    }

    // This function is executed if the above call fails

    function onGetListCountFail(sender, args) {
        alert('Failed to get list count. Error:' + args.get_message());
    }
})();

Sunday, April 5, 2015

Create your first app free in cloud Office 365 SharePoint 2013

Microsoft has provided an online free developer environment for creating Apps in SharePoint 2013.

In order to create an app below are the keys things we need to keep in mind

1) The site should be created using Developer site template.

2) When the developer site is created we would need to browse to SharePoint Store to add the NAPA tool as an app.

Start here.

https://msdn.microsoft.com/en-us/library/office/fp179924.aspx

Thursday, December 5, 2013

Move a source file and its properties from document library into a folder in same document library, delete the source and replace the destination file with another file

               using (SPSite oSite = new SPSite("http://Sharepoint2010:99/TestSite"))
                {
                    using (SPWeb oWeb = oSite.OpenWeb())
                    {
                                    //source file url
                                    string url = oWeb.Url + "/" + "SourceDocLib" + "/" + "SourceFileName";
                                   string fileName = "TestFile.docs"
                                    SPFile file = oWeb.GetFile(url);
                                    byte[] fileBytes = item.File.OpenBinary();

                         /destination document library
                          SPList docLib = oWeb.Lists.TryGetList("TestDocLib");
                          SPListItemCollection listItemCollection = docLib.Items;
                          string folderName = "TestFolder";
                          //get absolute destination Url
                          string folderUrl = oWeb.Url + "/" + docLib.Title + "/" + folderName + "/" + fileName;

                foreach (SPListItem listItem in listItemCollection)
                {
                    if (listItem["Name"] != null && listItem["Name"].ToString() == fileName)
                    {
                       // copy the source file and its properties to destination location
                        listItem.CopyTo(folderUrl);
                       //delete the source item
                        listItem.Delete();
                        break;
                    }
                }
                replace the destination file with another file
                SPFile destFile = oWeb.Files.Add(folderUrl, fileBytes, true);
                destFile.Update();

People picker fields promoted from Infopath to sharepoint list as text fields

When people picker fields are promoted from infopath to sharepoint list or form library, they are created in the sharepoint as "single text line" fields. In order to have them as people picker fields,  create new people picker fields in sharepoint form library and write an event receiver and in the item adding event assign the values from the single text line fields to the people picker fields.

Retract and redeploy timer job

1) Retract the timer job using powershell conmands
2) run net stop SPTimerv4 in stsadm
3) do IISRESET in cmd
3) Deploy the timer job using powershell commands

Programmatically assigning multiple users to people picker field Sharepoint 2010

SPFieldUserValueCollection usernameCol = new SPFieldUserValueCollection();
 
SPsite osite = new SPSite(http://sharepoint2010/testing);
SPWeb oWeb = osite.OpenWeb();
SPList oList = oWeb.Lists["TestList"];
SPListItemCollection oListItemCol = oList.Items;


foreach (SPListItem items in oListItemCol)
{

SPUser user = items["PeoplePickerfield"];

if (user != null)
{
usernameCol.Add(
new SPFieldUserValue(user.ParentWeb, user.ID, user.Name));
}

}
item["MultiplevalueallowedPeoplePickerField"] = usernameCol;
item.Update();




 

Check if the file exists in the document library SharePoint 2010

using (SPSite oSite = new SPSite(http://Sharepoint2010:100/Testing))

{

using (SPWeb oWeb = oSite.OpenWeb())

{

string fileName = "Test Reports.docx";

string documentLibraryName = "Test Reports";

string fileUrl = string.Format("{0}/{1}/{2}", oWeb.Url, documentLibraryName, fileName);

SPFile file = oWeb.GetFile(fileUrl);

string workfowStatus = "Approved";

//code when all the approvers approve delete this file and move it under folder called pdf

if (file.Exists)

{
Console.WriteLine("File {0} exists in the site", file.Name);

if (workfowStatus == "Approved")

{

file.Delete();

}

}

 
else

{Console.WriteLine("File Not Found");

}

}

}