Retrieving User Name in Sharepoint? - sharepoint

How does Sharepoint retrieve the user's actual name as displayed in the top right corner? e.g Welcome John Smith
I need to call this name as a variable or parameter on custom code in the XSL editor but I can't figure out how I can retrieve it, is it a global variable?

You can get a user's account name using the server variable LOGON_USER. However, this doesn't return a user's Display name.
I was able to get something working which a combination of web parts to display their name:
Add a UserContextFilterWebPart (you may need to enable this web part in the web part gallery).
Add a DataView webpart which queries the GetUserInfo method (part of the UserGroup.asmx web service).
a. Click "Connect to a web service..." in the Data Source Library pane under XML Web Services
b. Enter the Service description location (the URL to the UserGroup web service). Ex: http://server/sites/SiteCollection/SubSite/_vti_bin/UserGroup.asmx?WSDL
c. Click Connect (or reconnect)
d. Choose GetUserInfo for the Operation dropdown (the other dropdowns should be ok)
e. Modify the userLoginName parameter and check the box to allow the value to be set via web part connection. I also added a default value to test (ex: domain\login).
f. Click OK.
g. Click on the data source and click Show Data
h. Choose the columns you need and then drag them onto the page
Connect them together using web part connections (UserContext provided to the DataView).

If you want to use SPServices (which is great, btw):
function getCurrentUsersName(){
var firstName = $().SPServices.SPGetCurrentUser({
fieldName: "FirstName",
debug: false
});
return firstName;
}
function getCurrentUsersLastName(){
var lastName = $().SPServices.SPGetCurrentUser({
fieldName: "LastName",
debug: false
});
return lastName;
}
You can look up a host of other similar fieldnames here:

I believe it's coming in through NTLM Authentication/Active Directory. I ususally get login name DOMAIN/User in the HttpContext.Current.User.Identity.Name field, and then match against Active Directory and bring back the actual name of the user.

SPContext.Current.Web.CurrentUser.LoginName will give you the value for the login name of the current user, as displayed in the top right of a standard portal.
If you want to use this with XSLT, you need to find a way of assigning this to an XSL parameter value at run-time.

Related

Acumata Web Service Endpoint add Vendor Locations

I'm trying to use a Contract API application to edit and maintain vendor Locations.
I'm slightly confused on how I add a detail to the web service endpoint. I can easily add fields, but it seems when I try to add a new element it's not actually loading anything.
Here is what I did:
Added Object with name "Locations" and type "Detail"
Under that, added a top level call "Location" with screen id "Customer Locations"
Added fields to that
I used this VB code:
VendorFind = soapClient.Get(New Vendor With {.ReturnBehavior = ReturnBehavior.All,
.VendorID = New StringSearch With {.Value = "V01026"},
.Locations = New Locations() {New Locations With {.ReturnBehavior = ReturnBehavior.All}}})
It compiles and works but doesn't load the locations.
Anyone have any thoughts on how I achieve this?
For your custom endpoint you might want to use the 'Populate' option on the web service endpoint screen and select the required fields . Use the Vendor Location summary to get header information like vendor, location name etc and then select the details by selecting the correct mapped object for instance "General Info--> Location Contact " . This works fine and populates location details correctly for a selected vendor using postman tool . I am not much familiar with Vb but I suppose the issue might be in the way the endpoint entities are extended.

SharePoint 2013 Online - How to set up a filtered column linked to itself

I am a newbie with SharePoint. I have set up a document library. One of the columns is a unique id for a document. Another column called Related Document is a lookup field that may contain a clickable link to another document's unique id.
How to automatically fill in related document column with the link to the original document? That is, if I make document A be related to document B, I would like to automatically add a relationship from B to A as well. Not sure if it's possible to do with Related items feature - it does not seem to allow a clickable link.
Thank you.
The only way I can realistically see this being done is with a Remote Event Receiver. https://msdn.microsoft.com/en-us/library/office/jj220043.aspx
I can't think of a way out of the box that would do this.
You will need to create a SharePoint Add-in and deploy it to your SP Online instance. The remote code will get hosted on an Azure instance.
The remote code will get triggered when a document is updated.
You can then get a reference to the related document and fill in the related document link field accordingly.
You can pass parameters with the source parameter of SharePoint. This is actually to forward an URL to jump back to, but can be used to automatically pass parameters to the second form of the library.
Here is a small function that opens an upload dialog e.g. to be inserted in a content editor WebPart:
function openUploadDialog(passParameterName, passParameterValue)
{
var dialogOptions = SP.UI.$create_DialogOptions();
dialogOptions.url = "/_layouts/15/Upload.aspx?List=[INSERT_LIST_ID_HERE]&RootFolder=&IsDlg=1&source=%2fSitePages%2f[SOME_SITE_OF_YOURS].aspx%3f" + encodeURIComponent(passParameterName) + "%3d" + encodeURIComponent(passParameterValue);
dialogOptions.width = 700;
dialogOptions.height = 310;
dialogOptions.title = "Submit Document";
dialogOptions.dialogReturnValueCallback = Function.createDelegate(null, CloseThisDocCallBack);
SP.UI.ModalDialog.showModalDialog(dialogOptions);
}
openUploadDialog([NAME_OF_YOUR_ID], [VALUE_OF_YOUR_ID])
Short:
Add a field with the ID (or whatever you want) to your Library
Create a Content Editor or script WebPart where every you want and use the
function to open a dialog
look at the source of this webpart to find out the DOM ID of the field
Add another webpart to your Upload Form (Ribbon => Library => Form Webparts => Default Editor Form) to take the value from the source paramter (e.g. via JQuery) and write it into the new field you've just created.
Something like this:
id = GetUrlKeyValue('[NAME_OF_YOUR_ID]');
$('#[DOM_ID_OF_YOUR_CUSTOM_FIELD]').val(id);
I used this once to add an ID of a list element to the file. Hope that this is what you were looking for.

How to restrict SharePoint WSS3 users to edit only fields that are in their dedicated views

I have a list with the fields: Title, Client, Project, Description.
There is a view for analysts with the fields visible: Title, Project, Description.
All is fine so far as the analysts work with their views and not with the lists. But when they need to modify the records clicking on Edit, they see and able to modify the 'Client' field too.
How to prevent 'Client' field to be available for editing by the group? is there a way in WSS or I need to look for 3rd party list components?
All fields have a set of properties that determine their visibility in forms, such as "ShowInNewForm", "ShowInEditForm", and "ShowInDisplayForm". There's also some for the file dialog, the list settings page, and a few other places, but that's getting past it. Short answer, yes, you can make the field not show up in the edit form with WSS without needing any 3rd party components.
If you need a field that cannot be seen in the Edit Form by anyone (that is, no one should be able to have it in their form), then you need to modify "ShowInEditForm" to be true. This can't be modified directly through the SharePoint UI, but it is extremely simple using the object model.
If you need certain people to edit it at some point through the SharePoint UI, then you'll instead have to create a custom edit form. That's a bit more complex, so I'll hold off on providing that instruction unless you state you need to go down that route (or someone else passes by this answer and requests it). Nevertheless, it is fully possible with WSS 3.0.
EDIT
If you know already know how to insert inline C# code into an ASPX page, you can perform this very simply using SharePoint Designer. First, follow the instructions from this article, especially make sure you don't delete the default list form web part. Now, in the custom list form you added, make it include every field which anyone will be capable of editing. The last step is to make the form hide those fields for certain people. Let's default them to Visible=false, and flip this switch if the user is allowed them. You can do this either by checking if the current user is part of specified groups, or by checking if the user has a certain permission level only held by people of those groups. You'll basically write some code like the following, I'll use checking for a specified group as the example.
using (SPWeb web = this.Web)
{
SPUser currUser = web.CurrentUser;
string[] listOfGroups = { "Group1Name", "Group2Name", "Group3Name" };
foreach (string groupName in listOfGroups)
{
if (currUser.Groups.Contains(groupName))
{
//Repeat this for each Control, refer to them by their ID. For example, this is for a control with the ID txtTitle.
txtTitle.Visible = true;
}
}
}
If you don't know inline code, you'll have to write a custom ASPX page with a code-behind. Copy EditForm.aspx into a new file - you should do this after setting up a Custom List Form as per the article. You could also build a new ASPX page from scratch, but make sure you include all of the necessary Content placeholders for SharePoint pages. Now, the page currently inherits from Microsoft.SharePoint.WebPartPages.WebPartPage. We need to create custom code that inherits from that class, and change the page to inherit that new custom code instead. In the custom code, override one of the OnLoad or OnInit methods, and include your check for the user's permissions there as detailed earlier. Compile the code, deploy it to your SharePoint server, and it should be functional.
If you want to set fields hidden or display them in new form or edit form page of the list...
Go to the list settings.
In Advanced Settings, enable "Allow management of content types"
By doing so, you will get a List name Link on the List Setting Page.
Open the link and select the fields that you want to hide or uhide using add or remove option.
After saving this, again disable "Allow management of content types" in Advanced Setting...
Thats it :)))

Sharepoint List Filter by Profile Property

How would you filter a list in Sharepoint (WSS 3.0) by a current user's profile property. For example, I have a list with a Department column and I want to filter the list based on the current user's department (which would be a user profile's property).
Any idea's on how to do this?
I believe this is a repeated question filter a sharepoint list.
Anyway here you have mijn twee centen.
Try adding a tasks web part in your page and add a column "Department" to this tasks web part and enter some value, say 'IT'. Then add a UserContextFilter web part to same page. Configure it so that it sends "Department" user profile value to Tasks web part. At this point, when you try to connect usercontextfilter wp to tasks wp, you won't be able to see "Department" in drop down list. Beacuse this drop down list values are populated based on of tasks web part.
What you'll need to do is first get the user's profile, look up the property, and then use that in your CAML, like so:
UserProfile userProfile = ProfileLoader.GetProfileLoader().GetUserProfile();
string dept = userProfile["department"];
// SPQuery CAML stuff here

Groovy session. How to find the user name

I am developing an application under Groovy using scafoldig.
We are using the Acegi plugin for security.
We have a class called ChangeManagement defined as follows (simplified version)
class ChangeManagement {
static constraints = {
company(nullable:false)
lastModifiedByUser(nullable:false)
}
Company company
static belongsToCompany = [company:Company]
User lastModifiedByUser
static belongsToUser = [lastModifiedByUser:User]
}
When I address the create method I will get a pull-down menu
with the label "Last Modified By" with all the users from the table Users,
which is the standard behaviour.
Instead of this I would like to have directly the user who is already logged in and
no pull down menu. I've already generate the create template and I can modify it.
I am assuming that I have to get the username from the session.
If my assumption is true, how to get this username? and if I am wrong then from where can I get the username of the current user?
Thanks in advance.
See this question for details on how to get the current username.
I'd also suggest looking into Grails Filters for one way to easily stuff the user into the model after all controller calls and make it available in the view. I use this on my app to put the name of the logged in user in the upper right hand corner of the screen.

Resources