SharePoint Directories/Files - sharepoint

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);
}
}
}

Related

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 .

Get file size using CSOM in SharePoint 2010

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.

How to get data from web part in sandbox solution Sharepoint 2010

I want to get data from web part using sharepoint workflow to fill list item data. How can I solve this problem using sandbox solution?
I found how that can be done using farm solution but not sandbox:
using (SPSite site = new SPSite(""))
{
using (SPWeb web = site.OpenWeb())
{
SPFile file = web.GetFile("default.aspx");
SPLimitedWebPartManager lwpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared); SPLimitedWebPartCollection webParts = lwpm.WebParts;
System.Web.UI.WebControls.WebParts.WebPart wp = webParts[1];
string content = (((Microsoft.SharePoint.WebPartPages.ContentEditorWebPart)(wp)).Content).InnerText;
}
}

SharePoint 2007 : Get all list items in a list regardless of view from web service?

I need to check for duplicates. Currently I have items stored in sub folders in a list.
How can I retrieve all items in the list from a web service, so I can check for duplicates?
Here is code from object model: I want to do exactly this, but from a web service
private static void PrintItemTitles()
{
string strUrl = "http://localhost:8099/";
using (SPSite site = new SPSite(strUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["MyList"];
SPListItemCollection items = list.Items;
foreach (SPListItem item in items)
if (item != null)
Console.WriteLine(item.Title);
}
}
}
Use SPList.Items doesn't return all items? Well, then try SPList.GetItems(SPQuery).
Have a following SPQuery:
SPQuery query = new SPQuery();
query.ViewFields = "<FieldRef Name='ID'/><FieldRef Name='Title'/>";
query.Query = String.Format("<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>{0}</Value></Eq></Where>", someItemTitle)
query.MeetingInstanceId = -1; //In case if you query recurring meeting workspace - get items from all meetings
query.RowLimit = 10; //Will you have more than 10 duplicates? Increase this value
query.ViewAttributes = "Scope='RecursiveAll'"; //Also return items from folders subfolders
Note: There could be some mistakes in code, because i`m writing from top of my head
By executing this query and if it returns more than one item, then you have a duplicate!
Edit: Ahh, sorry, you are talking about Web Services.
Then this code won't help. There are 2 options then:
Option 1: You CAN create a view that
does include items even from folders
(flat view). See here for
instructions.
Option 2: According to Lists Web Service's GetListItems method, you can pass QueryOptions parameter. Pass in
<QueryOptions>
<MeetingInstanceID>-1</MeetingInstanceID> <!-- Again, if you query recurring meeting, you want ALL items -->
<ViewAttributes Scope='RecursiveAll' /> <!-- or Recursive if that does not work -->
</QueryOptions>
Good luck!
You can use the Lists.asmx web service, but that is quite hard to do as it returns quite a lot of information. I would deploy a custom web service on my SharePoint environment that contains that code and returns the list items.

Can I programmatically replace one webpart with another in Sharepoint?

Edit: This is a decade old so very likely not to be relevant to anyone, if google has brought you here I would be sceptical of the content against more modern frameworks!
We have a sharepoint website that has been quite heavily developed with content using the out of the box content editor webpart. This is a bit rubbish when it comes to using any other browser than IE.
We have in mind to update to the free Telerik content editor, however we would like to save ourselves a large amount of copy and pasting, and clicking and selecting to swap the web parts over.
There seems to be no official upgrade path but it seems to me that it must be possible to use the power of code (tm) to grab the content of the original webpart, new up a telerik webopart and put the content into the new webpart... then delete the old original web part.
Has anyone done this sort of thing? How is it best to lay out the code and actually run the code that will do the swap (if it is possible). A fresh webpart with a "Do Swap" button on? or something more sophisticated?
I also would need to walk through every page and subsite (and page in subsite) and look at every web part. Is there a best practice way of doing this?
I would write a console application for this.
Open the root Web and iterate through its sub webs.
Do the work needed for each page, by opening the WebPartManager.
Here are some pseudo code to get you started.
string SourceUrl = "http://ThisWeb";
using (SPSite sourceSite = new SPSite(SourceURL))
{
using (SPWeb sourceWeb = sourceSite.OpenWeb(SourceURL))
{
IterateSubWebsProd(sourceWeb):
}
}
private void IterateSubWebsProd(SPWeb sourceWeb)
{
// This is the migration function
DoThingyWithThisWeb(sourceWeb);
foreach (SPWeb subWeb in sourceWeb.Webs)
{
IterateSubWebsProd(subWeb);
subWeb.Dispose();
}
}
private void DoThingyWithThisWeb(SPWeb sourceWeb)
{
PublishingPage currentPage = null;
string currentPageName = "<something>";
// Find the pages that you want to modify, with a CAML query for example
SPQuery query = new SPQuery();
query.Query = string.Format("" +
"<Where>" +
"<Eq>" +
"<FieldRef Name='FileLeafRef' />" +
"<Value Type='File'>{0}</Value>" +
"</Eq>" +
"</Where>" +
"", currentPageName);
// This codesnippet is from a Publishing web example
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(sourceWeb);
PublishingPageCollection pageColl = publishingWeb.GetPublishingPages(query);
if (pageColl.Count > 0)
{
currentPage = pageColl[0];
}
using (SPLimitedWebPartManager wpMan = currentPage.ListItem.File.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
{
foreach (WebPart wp in wpMan.WebParts)
{
if (wp.GetType().Equals(typeof(Microsoft.SharePoint.WebPartPages.ContentEditorWebPart)))
{
Microsoft.SharePoint.WebPartPages.ContentEditorWebPart thisWebPart = wp as Microsoft.SharePoint.WebPartPages.ContentEditorWebPart;
// This is just dummy code, here you will do your content migration
XmlDocument xmlDoc = new XmlDocument();
XmlElement xmlElement = xmlDoc.CreateElement("RootElement");
xmlElement.InnerText = sourceItem[SourceField].ToString();
thisWebPart.Content = xmlElement;
wpMan.SaveChanges(thisWebPart);
}
}
}
}
I'm not 100% certain on this, but if your content is saved into a list as individual fields, could you not point the Telerik controls at where that content is saved? This may cause Telerik controls to freak out a bit on the first edit of that content, but they should be ok to re-format the content and recover.

Resources