Display document library setting as a report - sharepoint

I have a sharepoint site which contains site and subsite and document libraries in it. Couple of document library has setting to maintain the versioning of the doc along with the comments.
Now I have requirment where a client wants to see this settings site wise as, under which site how meny doc libraries are there which have versioning enabled...?
I want to show this information as an report.
Do I need to write a custom webPart or code for it ? Or how can I show this information as a report in sharepoint.
Thanks in advance.
Sachin

Versioning information is a property of the SPList class even though only document libraries can use versioning in SharePoint.
How you output this is up to you but here is some quick code to get you started.
Use the SPWeb.GetListsOfTypeMethod(SPBaseType.SPDocumentLibrary) to return an SPListCollection, loop through the list collection checking for the SPList.EnableVersioning property.
//Get your SPWeb whichever way works best
SPListCollection lists = web.GetListsofType(SPDocumentLibrary);
foreach (SPList list in lists)
{
if(list.EnableVersioning = true)
{
// Write to a list or update a count
}
//Output count results or a list of the doc libraries
}
Cheers, CJ

Related

Can we query and fetch data from LIST between different subsites using CAML Query.?

Have LIST created under one site, can the same LIST be used to fetch data from other site using CAML Query.?
eg:
Consider a LIST "xxx" created under SitePages/AAA/Lists/
Can i access the LIST "xxx" from other site i.e SitePages/BBB/
To Summarize, Is it possible to access the LIST across parent and child sites, vice-versa.
Thanks in advance
Yes it's possible. The only condition is to have the same domain (for example http://mydomain.com/SitePages/AAA/ and http://mydomain.com/SitePages/BBB/).
Then, with SharepointPlus library (http://aymkdn.github.io/SharepointPlus/symbols/%24SP%28%29.list.html#.get) that is a JavaScript API that would be (if you are on the http://mydomain.com/SitePages/AAA/* site) :
$SP().list("ListName","http://mydomain.com/SitePages/BBB/").get({
fields:"Title"
}, function getData(data) {
for (var i=0; i<data.length; i++) console.log(data[i].getAttribute("Title"));
});
Yes, as long as they are under the same site collection, you can use SPSiteDataQuery
Link to MSDN SPSiteDataQuery
SPSiteDataQuery allows you to set the Site collection under which to search the lists.

Get List Item by Guid without List in SharePoint

Could anybody help me to get List Item by unique Id in SharePoint without using List? I know the List Item unique Id, but I haven't information about List. Using Client Object Model I can do the following:
using (SP.ClientContext clientContext = new SP.ClientContext(...))
{
**SP.List list = clientContext.Web.Lists.GetById(listId);**
SP.ListItem listItem = list.GetItemById(listItemId);
clientContext.Load(listItem);
clientContext.ExecuteQuery();
...
}
Unfortunately, in this code I use listId, but the problem is that in my task I don't know listId, I have only ListItem Guid. Since I have unique Id, I believe that there must be a way to get this Item without using any additional information.
You've posted a snippet with a Client Object Model, so I suppose it's a preffered object model for the solution. However, if it's acceptable for You, I've found some information on how to achieve this using server side object model, which You may find helpful.
It seems that this can be done using SPWeb.GetFile(Guid itemGuid) if the item exist in the root site.
If you want it to work also for subsites, you can use SPWeb.GetSiteData method to invoke caml retrieving an item as explained in this article: http://www.chakkaradeep.com/post/Retrieving-an-Item-from-the-RootWeb-and-Subwebs-using-its-UniqueId.aspx.
You can also take a look at this thread on SO for some additional information:
SharePoint: Get SPListItem by Unique ID.
If you have any issues using those methods, let me know. I'll also look for alternatives for this to run with Client Object Model and I'll post them if I find some, however, it seems to be more difficult to achieve.

Programmatically adding columns to Sharepoint List Views

For a project I am working on, I am trying to do the following thing.
There's a Sharepoint 2010 environment, with a few Custom Lists created in Visual Studio. I am adding some fields to them using background code in the FeatureActivated function in the EventReceiver.
What I am doing there is adding a lookup field to the Sharepoint List, and setting the properties to allow it to lookup values from the other list I am adding to the Sharepoint Site.
However, I can't find a function to add it to one of the views. I've tried modifying the Schema Xml, but I can't seem to find a function to reinsert it to the List, and when using the Xml file from the View, I can't seem to make it work.
Is there an easy way to programatically add a field to a view? This would help me out, since there seems to be no way to do this correctly.
This can also be solved if one could explain my other question I have.
I would like to know how one could make Lookup fields in the Schema XML file. I have a Custom Content Type, and Custom Fields, and I am currently trying to look up the Naam field in the Intermediairs List. (This one is also created when deploying this solution). When searching Google, it seems I have to use either a name / the GUID of a List Instance here, but I don't know the GUID of the List Instance beforehand.
<Field ID="{7CC49D9D-F6F5-4A4A-851F-3152AAAAB158}" Type="Lookup"
List="Intermediairs" Name="IntermediairLookup" DisplayName="Intermediair"
StaticName="IntermediairLookup" Group="Onboarding" ShowField="Naam" />
You should know that this code seems to work:
SPWeb web = null;
SPSite site = null;
if (properties.Feature.Parent is SPWeb)
{
web = properties.Feature.Parent as SPWeb;
site = web.Site;
}
if (properties.Feature.Parent is SPSite)
{
site = properties.Feature.Parent as SPSite;
web = site.RootWeb;
}
web.AllowUnsafeUpdates = true;
SPList changeList = web.Lists.TryGetList("Onboarding");
SPList sourceList = web.Lists.TryGetList("Intermediairs");
if (changeList != null && sourceList != null)
{
changeList.Fields.Delete("IntermediairLookup");
var PrimaryColumnStr = changeList.Fields.AddLookup("Intermediair", sourceList.ID, true);
var PrimaryColumn = changeList.Fields.GetFieldByInternalName(PrimaryColumnStr) as SPFieldLookup;
PrimaryColumn.LookupField = sourceList.Fields["Naam"].InternalName;
PrimaryColumn.Update();
}
But yeah. I can't figure out how to do it in XML form. Anyone has any ideas? A solution to either of the questions would solve my core issue.
Greetings,
Mats
EDIT: Well, the question has now been answered, thanks again!
One thing though. I would really like to know at some point how to do something like this in XML / CAML. Does anyone know how to do that? Anyone who's still reading this thread?
Take a look at SPView.ViewFields
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spview.viewfields.aspx

Sharepoint Document Library Schema.xml Customization

Hi I am trying to add a custom field to the Schema.xml of Document library in sharepoint
here is the code that I took from a blog
In the ID i have to put the guid to do so
do I have to add my own guid or do i have to query the sharepoint database and find the guid and paste it there...
If i have to get it from sharepoint database which database and in what table I will find this information....
any help will be greatly appreciated
Thanks,
srikrishna.
This is now a duplicate post from MSDN forms
http://social.technet.microsoft.com/Forums/en-US/sharepointcustomization/thread/72df8c80-9e58-4c09-b1a6-ddfe1fb96b0a
there is a sure and safe mean :
1 - create a list with the SharePoint UI
2 - add a column to the list within the SharePoint UI
Create an applicative page that gets the schema of the list and you are done :
SPList mylist=SPContext.Current.Web.Lists["myNewList"];
string schema = mylist.SchemaXml;
schema = schema.Replace("<", "<");
schema = schema.Replace(">", ">");
string myAddedColumnSchema = mylist.Fields["MyAddedColumn"].SchemaXml;
myAddedColumnSchema = myAddedColumnSchema .Replace("<", "<");
myAddedColumnSchema = myAddedColumnSchema .Replace(">", ">");
Response.Write(schema);
Response.Write("<br>");
Response.Write("<br>"); Response.Write(myAddedColumnSchema );
Response.Write("<br>");
Response.Write("<br>");
This gives you the schema of the list with the field, and also just the field row in the list schema (of course copy the result in Visual Studio and use "Format Document" because on the web page it is just unreadable.
You cannot use this list schema by copying and pasting it in a SharePoint 2010 list Schema but you can locate the place where put the XAML correponding to your custom field.
After that you have to create a custom Content Type just for your custom field.
With SharePoint 2007 you could put a local content type corresponding to the custom field within the list schema but IT IS OVER. (I think... if someone can do it I will be pleased to be wrong ;-))
Then install the list feature with the new schema, and activate it within a site.
Then install the Content Type feature and activate it.
Then create a list based on the new schema, allow content type gestion for that list and add the new content type.
All the items based on the new content type will be allowed to use the new field.

Link data in custom SQL db with document library

Environment:
I have a windows network shared desktop application written in C# that leans against an MSSQL database. Windows sharepoint services 3.0 is installed (default installation, single processor, default sql express content database and so on) on the same Windows Server 2003 machine.
Scenario:
The application generates MS Word documents during processing (creating work orders) that need to be saved on sharepoint, and the result of the process must be linked to the corresponding document.
So, for each insert in dbo.WorkOrders (one work order), there is one MS Word document. I would need to save the document ID from the sharepoint library to my database so that later on, possible manual corrections can be made to the document related. When a work order is deleted, the sharepoint document would also have to be deleted.
Also, there is a dbo.Jobs table which is parent to dbo.WorkOrders and can have several work orders.
I was thinking about making a custom list on sharepoint, that would have two ID fields - one is the documents ID and the other AutoID of the document. I don't think this would be a good way performance-wise and it requires too much upkeep, therefore it's more error prone.
Another path I was contemplating is metadata. I could have an Identity field in dbo.WorkOrders that would be unique and auto incremented, and I could save that value as a file name (1.docx, 2.docx 3.docx ... n.docx where n would be the value in dbo.WorkOrder's identity field). In the metadata field of the Word document, I could save the job ID from dbo.Jobs.
I could also just increment the identity field in the WorkOrder (it would be a bigint), but then the file names would get ugly and maybe I'd overflow the ID range (since there could be a lot of documents).
There are other options also that I have considered and dismissed, since none of them satisfied the requirements (linked data sources, subfolder structures etc.). I'm not sure how to proceed. I'm new to sharepoint and it's still a bit of a mystery to me, as I don't understand all the inner workings of the system.
What do you suggest?
Edit:
I think I'll be using guid as file names and save those guids in my database after sending documents to sharepoint. What do you think of that?
All the documents in SharePoint under the same Content Database (SQL Database) are stored in the same table, that said, you have an unique ID for files no matter where they are in the sharepoint structure.
When retrieving files by their UniqueID The API only gives you the option to get them if you also know their SPWeb, so you could easily store, for each record you have in your external database (or your custom list, the SPFile GUID and the SPWeb GUID) retrieving them with:using(SPWeb subweb = (SPContext.Current.Site.OpenWeb(new Guid("{000...}")))
{
SPFile file = subweb.GetFile(new Guid("{111...}"));
// file logic
}
ps.: As Colin pointed out, url retrieval is possible but messy. I also changed the SPSite to the context since you are always under the same Site Collection in my example.
Like F.Aquino said, all items in sharepoint have a UniqueId field already (i.e. SPListItem.UniqueId and SPFile.UniqueId), which is a guid. Save that to your database, along with your web.'s guid. Then you can use the code provided by F.Aquino to get the file, or even the byte[] of the stream.
P.S. for F.Aquino, your code leaves the SPSite in memory, use this instead:
P.P.S this is just clarification, mark F.Aquino as the answer.
using(SPSite site = new SPSite("http://url"))
{
using(SPWeb subweb = site.OpenWeb(new Guid("{000...}"))
{
SPFile file = subweb.GetFile(new Guid("{111...}"));
// file logic
}
}

Resources