Monday 18 July 2016

AddAttachment CSOM SharePoint 2010

AddAttachment to Sharepoint 2010 List item CSOM


Below is the code  to add an attachment to sharePoint 2010 list item through CSOM with WebReference, 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using System.IO;
using System.Data;
using System.Net;

namespace CSOMFileUpload
{
    class Program
    {
        static void Main(string[] args)
        {
           
            const string listName = "Test";                   
            var credentials = new NetworkCredential("manjunath.soreddy", "MS", "dev");
            var clientContext = new ClientContext("https://spdev.com/TestSite/");
            clientContext.Credentials = credentials;
            var list = clientContext.Web.Lists.GetByTitle(listName);
            var itemCreateInfo = new ListItemCreationInformation();
            var newItem = list.AddItem(itemCreateInfo);
            newItem["Title"] = "Soreddy Client API";
            newItem.Update();
            clientContext.ExecuteQuery();
            clientContext.Load(newItem);
            clientContext.ExecuteQuery();          
            string path = @"c:\OutPutLog.txt";
           
//Here Uploadfile.inv is a Webreference which we have added to SharePoint 2010 Site from Visual Studio to add follow below steps
// Solution Explorer---> Add Service Reference ---> Advanced-> Add Web Reference --> URL

           var refin = new Uploadfile.inv.Lists();
           refin.Credentials = CredentialCache.DefaultCredentials;
           refin.Url = "https://spdev.com/TestSite/");
/_vti_bin/Lists.asmx";
           refin.AddAttachment(listName, newItem["ID"].ToString(), Path.GetFileName(path),
                                      System.IO.File.ReadAllBytes(path));
        }
    }
}



OutPut:





Load All UserProfile properties CSOM


Load All the UserProfile properties of a Logged in User 



Below code will load all user profile properties of a current logged in user in CSOM


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.UserProfiles;
using System.Net;


namespace UserProfilePropertiesCSOM
{
    class Program
    {
        static void Main(string[] args)
        {

            ClientContext ctx = new ClientContext("http://Sharepointdevsite:3636/");
            var cuser = ctx.Web.CurrentUser;
            ctx.Load(cuser); //Load the Current Logged User
            ctx.ExecuteQuery();       
            PeopleManager peopleManager = new PeopleManager(ctx);        
            PersonProperties userProperites = peopleManager.GetPropertiesFor(cuser.LoginName);
            ctx.Load(userProperites, p => p.AccountName, p =>
            p.UserProfileProperties);
            ctx.ExecuteQuery();
            foreach (var property in userProperites.UserProfileProperties)
            {
                Console.WriteLine(string.Format("{0}: {1}",
                property.Key.ToString(), property.Value.ToString()));
            }
            Console.ReadKey(false);

        }
    }
}

Ouput:





Monday 4 July 2016

Unknown SharePoint version: 16.0 Parameter name: version 0 0

Error while deploying app to SharePoint 2013 from Visual Studio



Solution :


Go to Project -> Project Properties from Visual Studio

 i.e In the Tools menu

Debub -> yourprojectName Properties  ( In mycase its TestApp Properties)














In SharePoint Tab -> Change the Version from SharePoint online to SharePoint 2013 and Click OK

Try to Rebuild and deploy the solution hope it should fix