UpdateListItem method and System Account - sharepoint

I've developed a Help Desk application in Visual Studio that uses a Sharepoint List to store the data.
My problem is that when i use the UpdateListItems method for submitting a new record, Sharepoint automatically assigns to the "Created By" (Author) field as "System Account" and this prevents the trigger of a workfow i created with Sharepoint Designer.
I've tried to force the Created By field during submit by adding to my batch person id;#person username
but it will still show "System Account"!!
Any ideas!?
sorry for my bad english:(

Have you considered using impersonation to update the list item?
You need a user on the site who has the correct access that you can impersonate. Look into the SPSite constructor that gets passed in an SPUserToken.
SPUserToken token = targetWeb.AllUsers["BobTheImpersonationAccount"].UserToken;
using(SPSite impersonatedSite = new SPSite("http://siteurl", token))
{
//Anything accessed through impersonatedSite and its child objects are now executing under the guise of BobTheImpersonationAccount
}

Option 1: "Update the workflow"
I would not recommend trying to change the Created By field. Instead, create a new People
field (named Submitter or something) in your list and populate that with the user. The workflow should use the Submitter field instead of Created By.
Option 2: "Update your code"
If your workflow, can't be updated, then you'll have to change your code. Instead of submitting using the Service Account, you will need to submit using the credentials of the current user. This means that they will need to have the correct permissions to the list. I am not sure if your code is in a web part or not, so I can't really be more specific.

Related

Set field via URL

Is it possible to have a field in the current item be changed by clicking a URL? The field would be a choice field with predefined choices.
Such as if the item field is currently:
Status: 2
If a user clicks the link, the field would now be:
Status: 3
If not, is there any other way for a user to easily change a field in the current item without actually haveing to visit the item?
Thanks!
Not Out Of The Box (OOTB) - but you've a few options.
Write an ASPX page to do what you want
Use something like SPServices and javascript to update the list item via the web services.
Use the Client Object Model (2010 only)
By the way - changing stuff on a 'get' can be dangerous as you can do malicious things - for example imagine you have a page that deletes the users account without any prompting (exact example doesn't matter) - what if someone clicks on that link by mistake or even worse what about an email sent with an image with that page as source URL - simply viewing the email could delete the users account.
It's not possible by using a GET request, but SharePoint 2010 is offering a RESTful API to manage ListItems from any client
The REST API is located within the virtual WebServices folgder under each SharePoint Site.
http://YourSharePointSite/_vti_bin/ListData.svc.
To perform an update on SharePoint ListItems you have to create a PUT Request. For more information on SharePoints REST API you should have a look at this MSDN site, there are also a lot of samples linked from this article.
Thorsten

How do I trigger a Sharepoint Workflow from code?

I have a workflow that is triggered by the creation of a list item and sends out an email when the list item is created.
If I create a new item in that list through the Sharepoint front end it sends the email.
I have a small console application designed to set the list item through the Sharepoint API. This is run as a scheduled task on a daily basis ( the purpose of the list is to nominate someone for a daily rota ) in the middle of the night. The scheduled task is run with the credentials of the Site Collection Administrator for the site.
The list item is added but the workflow is not triggered. In the log I get the following message:
Declarative workflows cannot automatically start if the triggering action was performed by System Account. Canceling workflow auto-start.
It appears as though the list item is being added by the system account rather than the user who is running the code. What do I need to do in order to have my code interact with Sharepoint using the same identity that is running the application?
Consider explicitly impersonating one of your SharePoint users (in this case the site collection administrator.) I answered a similar question over here: UpdateListItem method and System Account
Once you've created a SPSite object using the context of the impersonated user all operations against that object and its children will be performed as that user.
It might be better to set your second workflow to being manually started, and then start it programmatically (which your System Account -can- do):
There's probably a better way to do this, but this has been working for me:
// Look through all potential workflows for the correct one:
foreach (Microsoft.SharePoint.Workflow.SPWorkflowAssociation flowAssoc in SPContext.Current.Web.Lists["YourListName"].WorkflowAssociations) {
if (flowAssoc.Enabled && flowAssoc.AllowManual && (flowAssoc.Name.Trim() == workflowNameToLookFor.Trim())) {
// Start the workflow on the current item:
SPContext.Current.Site.WorkflowManager.StartWorkflow(SPContext.Current.Web.Lists["YourListName"].Items[itemIndex], flowAssoc, flowAssoc.AssociationData, true);
}
}

How to access the uiserID of the user who initiated a workflow

DUPE: See link below
I have a SharePoint Designer workflow and I need to send an email to the user who initiated the workflow. It is set for manual start only so it should always be running in the context fo a particular user.
I cannot see a SharePoint Designer action to let me do this, only to get the user who created the list item or by accessing the 'Modified By' column on the list. Neother of these is guaranteed to be the user who is calling the workflow.
Found the same question posted before. It appears to not be possible but the code for a custom workflow activity can be found here.

SharePoint - Adding users from Active Directory in a custom administration form

I have a project where I need to add users to a SharePoint portal, but when I add them, I also need to set addition parameters inside a separate database.
I want to add a custom administration screen where the administration can set these values when they add the user rather than forcing them to first add the user then go to a separate interface page where they set the values.
Does anyone know of any good articles that will explain how to accomplish this?
Thanks.
It would be easier to create a custom asp.net form that would get all the information required about the user.
the submit could then add the information to the database that is needed and use the object model to add the users.
SPRoleAssignment MyRoleAssign = new SPRoleAssignment(”domain/alias”, “email address”, “User Name”, “Description”);
SPRoleDefinition MyRoleDef = newSubWeb.RoleDefinitions["Contribute"];
MyRoleAssign.RoleDefinitionBindings.Add(MyRoleDef);
site.RoleAssignments.Add(MyRoleAssign);
Code from farhanfaiz.wordpress.com here
Otherwise the SharePoint webservices may do.
Examples here

How can I validate within an InfoPath form whether a user exists within a SharePoint Portal?

I am creating a form within InfoPath which is to be integrated into a SharePoint 2007 Portal. Within this form there will be a textfield into which a user can enter the Name of a Person.
How can I validate whether this Person exists or not?
Instead of validating the user, is there a way to fill a dropdown List with all usernames of the portal? (which of cause would be users from the Active Directory)
I haven't done this specifically, so there may be a better way, but I've been pulling a lot of data out of SharePoint and into an InfoPath Form (deployed to a SharePoint forms library and accessible through SharePoint Forms Service with MOSS Enterprise) and also going the other way using the SharePoint web services - very quick to use, and the person web service is right there.
Have you tried looking at the Contact Selector (an ActiveX control). Here is a MSDN-article describing how to add it as a control in InfoPath and this one describes how to make it work.
I have been using it in the majority of my infopath projects and it works flawlessly - also for browser-enabled forms.
When doing something similar in an ASP.NET application, I've used the Sharepoint search and searched the "People" Scope for the specific user. You can also search across profile information so you can pull back everyone with a certain Job Title, or in a specific Department.
I don't validate a person's existance, but I do determine a person's full name using their login and SharePoint. You should be able to modify this code for your purposes, it is below. For it to function you need a data connection in your InfoPath document called GetUsersFromSP. Configured as follows:
Location is - http://njintranet2/_vti_bin/usergroup.asmx?WSDL
Operation is – GetUserColectionFromSite (last one on list)
Automatically retrieve data when form is opened should be checked.
string ADName = System.Environment.UserName;
IXMLDOMDocument3 UserQuery = (IXMLDOMDocument3)thisXDocument.GetDOM("GetUsersFromSP");
UserQuery.setProperty("SelectionNamespaces",
"xmlns:dfs=\"http://schemas.microsoft.com/office/infopath/2003/dataFormSolution\" " +
"xmlns:tns=\"http://schemas.microsoft.com/sharepoint/soap/directory/\"");
((WebServiceAdapterObject)thisXDocument.DataAdapters["GetUsersFromSP"]).Query();
IXMLDOMNode Users = UserQuery.selectSingleNode("//dfs:myFields/dfs:dataFields/tns:GetUserCollectionFromSiteResponse/tns:GetUserCollectionFromSiteResult/tns:GetUserCollectionFromSite/tns:Users");
foreach (IXMLDOMNode current in Users.selectNodes("tns:User"))
{
string Login = current.attributes.getNamedItem("LoginName").text;
Login = Login.ToUpper();
if (Login.EndsWith(ADName.ToUpper()))
{
thisXDocument.DOM.selectSingleNode("my:root/my:config/my:User").text = current.attributes.getNamedItem("Name").text;
break;
}
}
Use this control:
http://blogs.msdn.com/infopath/archive/2007/02/28/using-the-contact-selector-control.aspx
Or if you want to build your own validator, you'll need to query the SharePoint profile database. I'd recommend this over querying AD directly. There's lots of articles online about working with the profile database.
Have a look at this Link, it explains how to populate a dropdown with the SharePoint Users
http://blueinfopath.blogspot.com/2008/10/how-to-populate-list-with-sharepoint.html
I you want to validate,
- Make a textbox
- Add a Button, name it ValidateUser
- Create a Receive Connection to the ......
- Att Rules to the ValidateUser
- Add the textbox to the field AccountName in the Secondary Datasource
- Execute the receive connection
- Get the value of the field Value with filter Name="PreferredName"
This work for Infopath Form Services
Test it and enter the UserLogin into the textbox and click on the Validate Button
Frederik

Resources