Get file size using CSOM in SharePoint 2010 - sharepoint

I need to get the file size information from a document library in SharePoint 2010 using C# managed CSOM. Any body can give some hint or share sample code?

The following code gets the size of the files in document lib
using (ClientContext oContext = new ClientContext("siteurl"))
{
oContext.Credentials = new NetworkCredential("LoginId", "pwd", "domain");
List oList = oContext.Web.Lists.GetByTitle("DocLib");
CamlQuery oQuery = new CamlQuery();
ListItemCollection collListItem = oList.GetItems(oQuery);
oContext.Load(collListItem);
oContext.ExecuteQuery();
foreach (ListItem oListItem in collListItem)
{
oListItem["File_x0020_Size"]
}
}
Let me know if it helps.

Related

SharePoint Directories/Files

I have a need that for a given sharepoint site, which i have read access to, i need to find out all the list/count of subdirectories & files. The folders go down 3-4 levels.
I have gone through this website for some options -
https://www.softvative.com/blog/2015/02/four-ways-to-get-report-of-sharepoint-folders-and-files-for-a-library/
Still not able to get a list, can anyone suggest a simple way to get that.
Thanks.
The following C# code for your reference.
using (SPSite site = new SPSite("http://sp2013/sites/team"))
{
using (SPWeb web = site.OpenWeb())
{
SPListCollection docLibraryColl = web.GetListsOfType(SPBaseType.DocumentLibrary);
foreach (SPList list in docLibraryColl)
{
Console.WriteLine("-------------LibraryName:" + list.Title + "--------------");
//get all folders in the library
SPListItemCollection oFolders = list.Folders;
//get all files in the library
SPQuery query = new SPQuery();
query.ViewAttributes = "Scope=\"Recursive\"";
var files=list.GetItems(query);
Console.WriteLine("Folder Count:"+oFolders.Count+" | File Count:"+files.Count);
}
}
}

Read items from list in host-web from provider-hosted app

I have a provider hosted app. and I'm trying to get to the listitems in a list that is on my host-web.
I can get a list of all the lists I have. But when I try to get to the listitems, it's always empty.
var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
using (var clientContext = spContext.CreateUserClientContextForSPHost())
{
clientContext.Load(clientContext.Web, web => web.Title);
clientContext.ExecuteQuery();
ListCollection lists = clientContext.Web.Lists;
List list = lists.GetByTitle("TestList");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><RowLimit>100</RowLimit></View>";
Microsoft.SharePoint.Client.ListItemCollection items = list.GetItems(camlQuery);
clientContext.Load<ListCollection>(lists);
clientContext.Load<List>(list);
clientContext.Load<Microsoft.SharePoint.Client.ListItemCollection>(items);
clientContext.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.ListItem item in items)
{
Response.Write("<br />" + item.FieldValues["Title"]);
}
}
In the AppManifest.xml i added a 'full control' permission for list
In the AppManifest.xml add a READ permission for list and Web
Apparently I did everything correct. When installing I still had to say which lists it had full control on.
Your code is perfectly fine ....
Just you have to make change in "AppManifest.xml" file permission give site collection as full control as you are using host web .

Creating new site collection in Office 365 from an APP

I have a requirement to create a new site collection from within an App in Office 365 programmatically. What I mean by a new site collection, is that after creation, it should appear on the list of site collections under the Admin --> Sharepoint tab of Office 365. I tried using a similar code below within a sharepoint hosted app that i had created,
//create sp context and get root
var clientContext = new SP.ClientContext.get_current();
var rootWeb = clientContext.site.rootWeb();
this.clientContext.load(rootWeb);
this.clientContext.executeQUery();
//set web info
var webInfo = new SP.WebCreationInformation();
webInfo.set_webTemplate('YourTemplateName');
webInfo.set_description('Your site description');
webInfo.set_title('Your site tittle');
webInfo.set_url(siteUrl);
webInfo.set_language(yourLangCode);
this.rootWeb.get_webs().add(webInfo);
this.rootWeb.update();
// save site and set callbacks
this.clientContext.load(this.rootWeb);
this.clientContext.executeQueryAsync(
Function.createDelegate(this, this.OnSiteCreationSuccess),
Function.createDelegate(this, this.Error));
However this just creates a sub site under the site collection that hosts my App.
Any suggestions on how i could implement this would be greatly appreciated.
It can be done with SharePoint Object Model 2013, the functions you need are inside the assembly: Microsoft.Online.SharePoint.Client.Tenant.dll, which is located at C:\Program Files\SharePoint Client Components\Assemblies after you install the SharePoint Client Object model 2013.
There's not much doc on this, but SharePoint Online Management Shell has the command to create the site collection, so I think it can be done with C# and figured it out. The code snippet shows how to do it.
using System;
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;
using System.Security;
namespace SharePoint123
{
class Program
{
static void Main(string[] args)
{
//please change the value of user name, password, and admin portal URL
string username = "xxxx#xxxx.onmicrosoft.com";
String pwd = "xxxx";
ClientContext context = new ClientContext("https://xxxx-admin.sharepoint.com");
SecureString password = new SecureString();
foreach (char c in pwd.ToCharArray())
{
password.AppendChar(c);
}
context.Credentials = new SharePointOnlineCredentials(username, password);
Tenant t = new Tenant(context);
context.ExecuteQuery();//login into SharePoint online
//code to create a new site collection
var newsite = new SiteCreationProperties()
{
Url = "https://xxxxx.sharepoint.com/sites/createdbyProgram1",
Owner = "xxxxx#xxxxx.onmicrosoft.com",
Template = "STS#0", //using the team site template, check the MSDN if you want to use other template
StorageMaximumLevel = 100,
UserCodeMaximumLevel = 100,
UserCodeWarningLevel = 100,
StorageWarningLevel = 300,
Title = "CreatedbyPrgram",
CompatibilityLevel = 15, //15 means Shapoint online 2013, 14 means Sharepoint online 2010
};
t.CreateSite(newsite);
context.ExecuteQuery();
//end
}
}
}

creating document set programmatically in sharepoint server 2010

how to create document set in document library programmatically in sharepoint server 2010?
If you want to use client object model for this:
{
ClientContext clientContext = new ClientContext("http://<<SERVER_NAME>>");
Web site = clientContext.Web;
// Create a list.
ListCreationInformation listCreationInfo =
new ListCreationInformation();
listCreationInfo.Title = "Document Library";
listCreationInfo.TemplateType = (int)ListTemplateType.DocumentLibrary;
List list = site.Lists.Add(listCreationInfo);
// Enable Content Types on list
list.ContentTypesEnabled = true;
// Update List Configuration
list.Update();
// Send it to SharePoint
clientContext.ExecuteQuery();
// Get Content Type Document Set ID = 0x0120D520
ContentType ctx = clientContext.Site.RootWeb.AvailableContentTypes.GetById("0x0120D520");
// Add Existing To List
list.ContentTypes.AddExistingContentType(ctx);
// Execute
clientContext.ExecuteQuery();
}
http://msdn.microsoft.com/en-us/library/gg581064.aspx
Afterwards add an item of that contenttype.

how to write the SPQuery for getting all data of contact list?

I am writing the SPQuery for getting the data in contact list of sharepoint site.but how to write that?
Means I want to retrieve data as :
Name:aaa
Cell No: 13123131
Address : something address here..
so on...
of given LAst Name in search text box (build by me).
how to do that? Means what query i have to write? (Syntax please).
string siteUrl = "http://sharepointserver/";
string webUrl = "MySubSite";
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb(webUrl))
{
SPList list = web.Lists["Contacts"];
string lastName = "Smith";
SPQuery q = new SPQuery();
q.Query = string.Format("<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>{0}</Value></Eq></Where>", lastName);
SPListItemCollection items = list.GetItems(q);
foreach (SPListItem item in items)
{
Console.WriteLine(item["Title"]);
}
}
}
You can construct a caml query with a filter on LastName. Please check the below msdn link which has an example of using SPQuery with task list. Similarly you can use it for contact list as well.
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.aspx
You can use Caml query builder to construct your caml queries. YOu can download it from here:
http://www.u2u.be/Res/downloads/u2ucamlquerybuildersolution.zip
Hope this helps.
-Faiz

Resources