1
votes

I am new to sharepoint so if I made something wrong please correct me.

I have a document library which takes appointments (a document, informations of the customer such as name, phone, etc.). Then I created a site page and web part which takes those informations form customer and send it to the document library. When I fill that form and save it, list takes parameters very well. After that, I created an approval sharepoint workflow for that library to approve documents by some users. Problem starts here: When I add items to document list manually(by pressing add item) workflow column item(name of workflow) is generated automatically and works well. But when I try to add by code workflow item is not generating.

       public static void InsertIntoDocumentToLib( string name, int age,string path,SPContext context)
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (var site = new SPSite(context.Site.ID))
            {
                using (var siteCollection = new SPSite(context.Site.Url))   // site.RootWeb
                {
                    using (var currentWeb = siteCollection.OpenWeb(context.Web.ID))
                    {
                        SPList spList = currentWeb.Lists.TryGetList(DOCUMENT_LIB_NAME);
                        if (spList != null)
                        {
                            currentWeb.AllowUnsafeUpdates = true;
                            SPFolder myLibrary = currentWeb.Folders[DOCUMENT_LIB_NAME];
                            bool replaceExistingFiles = true;
                            string fileName = System.IO.Path.GetFileName(path);

                            FileStream fileStream = File.OpenRead(path);
                            SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);
                            spfile.Item[NAME_COLUMN] = name;
                            spfile.Item[AGE_COLUMN] = age; 
                            spfile.Item.Update();
                            myLibrary.Update();
                            currentWeb.AllowUnsafeUpdates = false;
                        }

                    }
                }
            }
        });

    }
1

1 Answers

0
votes

Workflows in SharePoint cannot be triggered by the System account, that's probably the username you in 'Created By'. When you created the list item manually (using add item) you created the item with your user account.

The easiest and safest thing to do is remove RunWithElevatedPermissions, but then only users that have permissions to create entries in the target list can use your page.

If your requirements say other users have to use your page as well, you'll have to use impersonation.

What you do is you use the credentials SPUserToken from a non-system user that does have permission to create an item in the list.

This blog article has an example on how to use SPUserToken.

You could also log in as a different windows user, but I don't think that should be necessary since you're running in a site page.

Extreme SharePoint also has a good overview of elevation/impersonation in SharePoint you may want to look at.