How to get SharePoint file creator name using AllDocs table? - c#

How can I get following information about a SharePoint document.
Created By
Last modified by
I am using alldocs table of sharepoint content database but can't find it very helpful.

If you are touching the alldocs table, you aren't using the object model. Further it isn't supported and is generally a bad idea. Here is some sample code to get that same information using the object model:
using (SPSite site = new SPSite("http://your.sharepoint.server/"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Documents"];
SPListItem item = list.Items[0];
string author = (string)item["Author"];
DateTime modified = (DateTime)item["Modified"];
}
}

Related

Add column values while uploading a file to Sharepoint with C#

Currently I'm working on a project that requires the upload of files to Sharepoint. So far i can upload files programmatically without problems:
class SPAPI
{
internal void SPUploader(Stream fs, string filename)
{
ClientContext context = new ClientContext("https://....de");
System.Net.ICredentials creds = System.Net.CredentialCache.DefaultCredentials;
context.Credentials = creds;
context.RequestTimeout = 60000000; // Time in milliseconds
string fnUrl = "/software/ap_ck/Dokumenten Management System/" + filename;
Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, fnUrl, fs, true);
}
}
Additionally i also have to add specific values (together with the file) to the columns in the Sharepoint document list. As this is the first time for me working with Sharepoint some of the solutions found online are a bit confusing.
One solution contains the following line:
List docList = site.Lists.GetByTitle("members");
I can only guess that the "Lists" are the storages for the files? How do i get the title of my list where i upload each files to? Sry for that stupid question, but i have never worked with Sharepoint before.
We can use Event receiver to achieve this.
First create a event receiver as below.
We can add column values after the item added(document uploaded) as below.
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Web;
using System.Text;
using System.Data.SqlClient;
namespace SharePointFarmSolutionDev.DocumentEventReceiver
{
/// <summary>
/// List Item Events
/// </summary>
public class DocumentEventReceiver : SPItemEventReceiver
{
private readonly HttpContext _currentContext;
public DocumentEventReceiver()
{
if (_currentContext == null)
{
_currentContext = HttpContext.Current;
}
}
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
//init
Guid listId = properties.ListId;
int itemId = properties.ListItemId;
SPSite site = properties.Site;
SPWeb web = properties.Web;
SPList list = web.Lists[listId];
SPListItem item = list.GetItemById(itemId);
item["title"] = "hello";
item.Update();
}
}
}
More information about Event Receiver, we can refer to: Introduction To SharePoint Event Receivers
I would suggest you highlight which version of SP you are working with to get a better answer. Once you have performed the above code you would need to get a handle of the object you have just uploaded.
Uri filename = new Uri("Your file Url");
string server = filename.AbsoluteUri.Replace(filename.AbsolutePath, "");
string serverrelative = filename.AbsolutePath;
Microsoft.SharePoint.Client.File file= web.GetFileByServerRelativeUrl(serverrelative);
ListItem lstItem = file.ListItemAllFields;
clientContext.Load(lstItem);
clientContext.ExecuteQuery();
Once you have got the list handle, you can update the editor field as below
lstItem[<Field Name Here>] = <value here>
lstItem.Update();
clientContext.ExecuteQuery();
I have stolen the answer from a blog here but this should be a good starting point.
It worth remembering when you are working with doc libs you have the file you upload and an associated ListItem, Your code deals with the upload and the piece of code I have copied deals with the list item.
I've not tested this but I'm pretty sure this is how you would have to do it.
Cheers
Truez

Sharepoint2013 Site - CSOM - Update Navigation Settings

We need to update Global and Current Navigation Settings for the site.
Below is our code
var publishingWeb = PublishingWeb.GetPublishingWeb(this.CC, subWeb);
// WebNavigationSettings
var webNavigationSettings = new WebNavigationSettings(this.CC, subWeb);
webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.PortalProvider;
webNavigationSettings.CurrentNavigation.Source = StandardNavigationSource.PortalProvider;
// CSOM don't have: publishingWeb.Navigation.GlobalIncludeSubSites.
subWeb.AllProperties["__GlobalIncludeSubSites"] = "True"; //TODO: Verify why it is not working.
subWeb.AllProperties["__GlobalIncludePages"] = "True"; //TODO: Verify why it is not working.
subWeb.Update();
webNavigationSettings.Update(tSession);
CC.Load(subWeb, WEB_INCLUDES);
// Apply the load
CC.ExecuteQuery();
As we are using CSOM, we did not have
publishingWeb.Navigation.GlobalIncludeSubSites
.
So we tried to set using AllProperties to set GlobalIncludeSubSites and GlobalIncludePages.
But those properties are not getting set.
Is there any way to fix this problem.
I went throught article http://discoveringsharepoint.wordpress.com/2013/03/19/programmatically-set-navigation-settings-in-sharepoint-2013/
But it uses namespace : Microsoft.SharePoint.Publishing.Navigation
But our's namespace is : Microsoft.SharePoint.Client.Publishing.Navigation
As we are doing from client server object model.
Is there any way to solve this ?
Thanks
In SharePoint 2013 was introduced a new Microsoft.SharePoint.Client.Publishing and Microsoft.SharePoint.Client.Publishing.Navigation namespaces in CSOM API. But unfortunately it is not supported to modify navigation settings using WebNavigationSettings class since properties are exposes as a read-only.
You could utilize the following approach for that purpose. ClientPortalNavigation.cs represents a CSOM counterpart for SSOM PortalNavigation Class.
The following example demonstrates how to utilize that class and update Navigation settings:
using (var ctx = new ClientContext(webUri))
{
var navigation = new ClientPortalNavigation(ctx.Web);
navigation.CurrentIncludePages = true;
navigation.GlobalIncludePages = false;
navigation.SaveChanges();
}
ClientPortalNavigation.cs is compatible with SharePoint 2010/2013
CSOM APIs.
References
Access and Manipulate Navigation Settings via SharePoint Client Object Model
ClientPortalNavigation.cs

How to get all the users in Zendesk

I have created users using CreateOrUpdateUser() method but i was unable to fetch all the users from zendesk. I am getting null for "oListUser" also I tried to fetch user list for Organization but for that also i am getting null.Any help would be appreciated. There is no issue with the connection.
Code:
ZenDeskApi.ZenDeskApi oZen = new ZenDeskApi.ZenDeskApi("https://waresolution.zendesk.com", "j#se.com", "87ggh76IO");
List<User> oListUser = oZen.GetUsers();
User oUsers = new ZenDeskApi.Model.User();
oUsers.Email = "r#se.com";
oUsers.IsVerified = true;
oUsers.Name = "R r";
oUsers..........// Other properties
int a = oZen.CreateOrUpdateUser(oUsers);
List<Organization> oOrg = oZen.GetOgranizations();
foreach (var orgItem in oOrg)
{
int orgId = orgItem.Id;
}
Take a look at this zendesk api client for C#.net on Github. See the JUSTEAT blog for more details. You can use this client to get all users like this:
Create a client:
IZendeskClient client = new ZendeskClient(
new Uri("my-zendesk-api-host-endpoint"),
"my-zendesk-username",
"my-zendesk-token"
);
Then you can use the Search resource to search all users:
var result = client.Search.Find(new ZendeskQuery<User>().WithCustomFilter("y", "x"));
You can download the code as a Nuget here
I am using ZendeskApi_v2 and I am able to get all the users using api as follows:
var userList = api.Users.GetAllUsers();
Here userList is GroupUserReponse.I doubt whether we have any method GetUsers().Atleast its not available in the version which I am using.Once you get the response you can iterate through it.
I see that this question is related to ZenDeskApi which is available at this location:
https://github.com/eneifert/ZenDeskApi.
Sorry,I have not worked on it and tried this.
Not sure if you got a response to this but for anyone else looking for info on the API:
Try instead of:
ZenDeskApi.ZenDeskApi oZen = new ZenDeskApi.ZenDeskApi("https://waresolution.zendesk.com", "j#se.com", "87ggh76IO");
ZenDeskApi.ZenDeskApi oZen = new ZenDeskApi("https://waresolution.zendesk.com", "j#se.com", "87ggh76IO");
Also, to get a paginated list of 100 users instead of the group response, just call:
var zendeskUsers = oZen.Users.GetAllUsers().Users;

Application to copy a SharePoint Group

As per this entry: Cloning a sharepoint rolegroup I'm trying to create a console application to copy a SharePoint group, including its permissions.
Based on the answer from Tjassens I've reached the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace REGroupCopy
{
class Program
{
static void Main(string[] args)
{
using (SPSite spSite = new SPSite("http://dev"))
{
using (SPWeb spWeb = spSite.RootWeb)
{
// first we find the group that we want to clone
SPGroup group = spWeb.Groups["Test Group"];
// then we use this retreived group to get the roleassignments on the SPWeb object
SPRoleAssignment ass = spWeb.RoleAssignments.GetAssignmentByPrincipal(group);
string groupName = "Test Group 2"; // group to create
string groupDescription = "Group created by REGroupCopy";
string user = "michael";
spWeb.SiteGroups.Add(groupName, user, user, groupDescription);
SPGroup newGroup = spWeb.SiteGroups[groupName];
SPRoleAssignment roleAssignment = new SPRoleAssignment(newGroup);
//add role to web
spWeb.RoleAssignments.Add(roleAssignment);
spWeb.Update();
}
}
}
}
}
Unfortunately I don't think I'm understanding everything correctly. Specifically, I think these lines are incorrect, but I'm unsure what they should be:
string groupName = "Test Group 2"; // group to create
string groupDescription = "Group created by REGroupCopy";
string user = "michael";
spWeb.SiteGroups.Add(groupName, user, user, groupDescription);
I don't necessarily need somebody to come along and fix this for me (after all ,this is a learning exercise). Instead, could you please help me to understand where my thought process is falling down and what I need to learn to rectify this?
You have found the correct problem with your code. When you call the following method:
spWeb.SiteGroups.Add(groupName, user, user, groupDescription);
you forgot that the user should not be a string but an actual SPUser object. If you get the SPUser object you should be able to add the new group to the SPWeb/SPSite.
you can get the user object by using for instance:
SPUser spUser = spWeb.EnsureUser(loginName);
Add method :
First Param : The new group name
Second param : The owner (SPUser object)
Third param : The default user for the group (SPMember object).
Fourth param : The new group description
From Site admin New group
first param is like Name TextBox
second param and third param is like Group owner people picker
fourth param is like About me RichTextBox

SharePoint 2010: How to extract personalized web part property for all users

Background:
I have a built-in-house custom SharePoint 2010 web part, inheriting from System.Web.UI.WebControls.WebParts.WebPart, which has a property defined like so:
[Personalizable(PersonalizationScope.User)]
[WebBrowsable(true)]
[WebDisplayName("...")]
[WebDescription("...")]
[Category("...")]
public string CustomProp { get; set; }
The Powers-That-Be would like to know the value of the property for each of its 3,000 users. I'm already familiar with how to use the SPLimitedWebPartManager to extract the property in either shared view or my own personal view, using code similar to:
var pageUrl = "/Pages/SomePage.aspx";
var limitedManager = SPContext.Current.Web.GetLimitedWebPartManager(
pageUrl,
PersonalizationScope.User); // or .Shared, if loading the shared value
var webPart = limitedManager.WebParts.OfType<MyWebPart>().FirstOrDefault();
var prop = webPart.CustomProp;
So, the question:
I'm stuck on collecting this information for all users. Assuming I already have, or know how to retrieve, a list of login names or SPUser objects, how do I go about retrieving the web part property for all users? I can use suggestions in C#, VB.NET, or PowerShell.
Try this code - I haven't tested it but I think it should work.
const string absolutePageUrl = "http://spsite:5000/Pages/SomePage.aspx";
foreach (SPUser user in SPContext.Current.Site.RootWeb.AllUsers.Cast<SPUser>())
{
if (!user.LoginName.StartsWith("DOMAIN NAME"))
continue;
using (SPSite site = new SPSite(absolutePageUrl, user.UserToken))
using (SPWeb web = site.OpenWeb())
{
var mgr = web.GetLimitedWebPartManager(absolutePageUrl, PersonalizationScope.User);
var webPart = mgr.WebParts.OfType<MyWebPart>().FirstOrDefault();
var prop = webPart.CustomProp;
}
}

Categories

Resources