I'm using the OneNote interop in C# to try and delete a page using the following block of code:
static string GetObjectId(string parentId, HierarchyScope scope, string objectName)
{
string xml;
onenote.GetHierarchy(parentId, scope, out xml);
var doc = XDocument.Parse(xml);
var nodeName = "";
switch (scope)
{
case (HierarchyScope.hsNotebooks): nodeName = "Notebook"; break;
case (HierarchyScope.hsPages): nodeName = "Page"; break;
case (HierarchyScope.hsSections): nodeName = "Section"; break;
default:
return null;
}
var node = doc.Descendants(ns + nodeName)
.Where(n => n.Attribute("name").Value == objectName)
.FirstOrDefault();
return node.Attribute("ID").Value;
}
static string DeletePage(string sectionId, string pageId, string pageName)
{
var pageId = GetObjectId(sectionId, HierarchyScope.hsPages, pageName);
onenote.DeleteHierarchy(pageId, DateTime.MinValue, true);
}
But each time I open OneNote the page remains.
I have been reading on the following page that this is the method I should be using however I don't appear to be having much luck: https://msdn.microsoft.com/en-us/library/office/gg649853(v=office.14).aspx
Can someone please point me in the right direction on how to delete a page in OneNote using the OneNote interop in C#.
This is an old question but since there's no answer... I have found that simply calling the functions as follows:
onenote.DeleteHierarchy(pageId)
works and will result in the file being sent to the recycle bin. If you want to fully delete the page, do the following:
onenote.DeleteHierarchy(pageId, deletePermanently:true)
Using the dateExpectedLastModified parameter is a bit tricky.
Per MSDN– (Optional) The date and time that you think the object you want to delete was last modified. If you pass a non-zero value for this parameter, OneNote proceeds with the update only if the value you pass matches the actual date and time the object was last modified. Passing a value for this parameter helps prevent accidentally overwriting edits users made since the last time the object was modified.
Related
Im trying to update a string field in specific document using Firebase Firestore for my android app but every method I see is while knowing the document refernce which I find difficult to find in my program.
Would like for some help for another method or help in finding the document refernce using a specific field value.
Thanks in advance.
(using C# btw)
private async Task<string> GetDocRefAsync(string userId)
{
Object obj = await FirestoreData.GetFirestore().Collection(DBConstants.FS_COLLECTION_USERS).
WhereEqualTo(DBConstants.FS_COLLECTION_USERS_USER_ID, userId).Get();
QuerySnapshot snapshot = (QuerySnapshot)obj;
if (snapshot.IsEmpty)
{
Log.Debug("UpdateGroupAsync", "userId: " + userId + " not found");
return null;
}
string docRef = "";
foreach (DocumentSnapshot item in snapshot.Documents)
{
//docRef = item.;
}
return docRef;
}
Firstly ive tried to find the document ref using this code but dont have a function to get the ref even after getting the correct document.
the fourth line from the bottom is where I couldnt find it.
database pic
this.groupCode = code;
string strUserRef = GetDocRefAsync(userRef).ToString();
DocumentReference docReference = database.Collection(DBConstants.FS_COLLECTION_USERS_GROUPS_CODE).Document(strUserRef);
docReference.Update(DBConstants.FS_COLLECTION_USERS_GROUPS_CODE, groupCode);
If you want to get the documents where a field has a given value, you can use a query. Then once the query returns, you can get documents IDs with the .Id field on each DocumentShapshot in the returned documents.
You will also need to add await for the returned value since it is an async method returning a Task<string> not returning a string.
private async Task<string> GetDocRefAsync(string userId) {
CollectionReference usersRef = FirestoreData.GetFirestore().Collection(DBConstants.FS_COLLECTION_USERS);
Query query = usersRef.WhereEqualTo(DBConstants.FS_COLLECTION_USERS_USER_ID, userId);
// or GetSnapshotAsync depending on the version of firebase
QuerySnapshot querySnapshot = await query.Get();
// Note: if it matches multiple documents this will just return
// the ID of the first match
foreach (DocumentSnapshot item in snapshot.Documents)
{
return item.Id;
}
Log.Debug("UpdateGroupAsync", "userId: " + userId + " not found");
return null;
}
And you can use it like this to update a document (note that you were using a different collection here - probably by mistake).
string userDocId = await GetDocRefAsync(userId);
CollectionReference userCollection = database.Collection(DBConstants.FS_COLLECTION_USERS);
DocumentReference docReference = userCollection.Document(userDocId);
// or UpdateAsync depending on your version of firebase
docReference.Update(DBConstants.FS_COLLECTION_USERS_GROUPS_CODE, groupCode);
I have a case that I need to log the chat history (I am capable of doing this already) and I need it to be logged in a text file (able to log it already).
The problem is the file is being accessed all over again so I need to somewhere store the filename of the file somewhere else, right now I have this code:
public async Task LogAsync(IActivity activity)
{
var conversation = "";
var convActivity = "";
var ctr = 0;
conversation = $"From: {activity.From.Name}\r\n To: {activity.Recipient.Name}\r\n Message: {activity.AsMessageActivity()?.Attachments}\r\n ";
fileName = "test";
await LogActivity(fileName, conversation);
}
The LogActivity is the one handling the append of the file. So what I need is I want the unique fileName to be instantiated once while appending the file all over again or rather while continuously accessing this method.
Or is there a way to log the chat history of bot once like if a Context.Done was called or before it?
Or the inefficient way I am thinking of was making use of .From.Name and .Recipient.Name
So the result will be:
if (activity.From.Name.ToLower().ToString() == "user")
{
name.Value = $"{activity.From.Name.ToString()}";
conversation = $"From: {activity.From.Name}\r\n To: {activity.Recipient.Name}\n Message: {activity.AsMessageActivity()?.Text}\n";
}
else
{
name.Value = $"{activity.Recipient.Name.ToString()}";
conversation = $"From: {activity.From.Name}\r\n To: {activity.Recipient.Name}\r\n Message: {activity.AsMessageActivity()?.Text}\r\n ";
}
await LogActivity(name.Value, conversation);
If I understand correctly, you just want to persist a value throughout a conversation; in this case a filename.
If that's correct, then you can store it in PrivateConversationData which lives in thecontext.
For example:
context.PrivateConversationData.SetValue<string>("log_filename", "log-name-here.txt");
For an example, check here: https://www.robinosborne.co.uk/2016/08/08/persisting-data-within-a-conversation-with-botframeworks-dialogs/
For a full example about persisting the whole conversation, this might also help: https://www.robinosborne.co.uk/2016/11/22/transcribing-messages-in-botframework/
Okay, rposbo's answer also works if you have your own logger (that's what I observed, or maybe if you can implement it the other way around, you can use it, it's up to you) that will persist on each every conversation you have with your bot, see the link he provided on how to persist the whole conversation. As for my end, I used dictionary to store the filename. So down below is what I did
public string _Name { get { return name; } }
string name;
public static Dictionary<string, string> fileName = new Dictionary<string, string>();
public void SetFileName(string _fileName)
{
var isCached = fileName.TryGetValue("filename", out name);
if (!isCached)
{
name = $"{_fileName}_{DateTime.Now.Ticks}";
fileName.Add("filename", name);
}
}
Btw, can I accept two answers? since rposbo's answer also works, but it just doesn't fit for me.
I have seen many articles about this but all of them are either incomplete or do not answer my question. Using C# and the OneNote Interop, I would like to simply write text to an existing OneNote 2013 Page. Currently I have a OneNote Notebook, with a Section titled "Sample_Section" and a Page called "MyPage".
I need to be able to use C# code to write text to this Page, but I cannot figure out how or find any resources to do so. I have looked at all of the code examples on the web and none answer this simple question or are able to do this. Also many of the code examples are outdated and break when attempting to run them.
I used the Microsoft code sample that shows how to change the name of a Section but I cannot find any code to write text to a Page. There is no simple way to do this that I can see. I have taken a lot of time to research this and view the different examples online but none are able to help.
I have already viewed the MSDN articles on the OneNote Interop as well. I vaguely understand how the OneNote Interop works through XML but any extra help understanding that would also be appreciated. Most importantly I would really appreciate a code example that demonstrates how to write text to a OneNote 2013 Notebook Page.
I have tried using this Stack Overflow answer:
Creating new One Note 2010 page from C#
However, there are 2 things about this solution that do not answer my question:
1) The marked solution shows how to create a new page, not how to write text to it or how to populate the page with any information.
2) When I try to run the code that is marked as the solution, I get an error at the following line:
var node = doc.Descendants(ns + nodeName).Where(n => n.Attribute("name").Value == objectName).FirstOrDefault();
return node.Attribute("ID").Value;
The reason being that the value of "node" is null, any help would be greatly appreciated.
I asked the same question on MSDN forums and was given this great answer. Below is a nice, clean example of how to write to OneNote using C# and the OneNote interop. I hope that this can help people in the future.
static Application onenoteApp = new Application();
static XNamespace ns = null;
static void Main(string[] args)
{
GetNamespace();
string notebookId = GetObjectId(null, OneNote.HierarchyScope.hsNotebooks, "MyNotebook");
string sectionId = GetObjectId(notebookId, OneNote.HierarchyScope.hsSections, "Sample_Section");
string firstPageId = GetObjectId(sectionId, OneNote.HierarchyScope.hsPages, "MyPage");
GetPageContent(firstPageId);
Console.Read();
}
static void GetNamespace()
{
string xml;
onenoteApp.GetHierarchy(null, OneNote.HierarchyScope.hsNotebooks, out xml);
var doc = XDocument.Parse(xml);
ns = doc.Root.Name.Namespace;
}
static string GetObjectId(string parentId, OneNote.HierarchyScope scope, string objectName)
{
string xml;
onenoteApp.GetHierarchy(parentId, scope, out xml);
var doc = XDocument.Parse(xml);
var nodeName = "";
switch (scope)
{
case (OneNote.HierarchyScope.hsNotebooks): nodeName = "Notebook"; break;
case (OneNote.HierarchyScope.hsPages): nodeName = "Page"; break;
case (OneNote.HierarchyScope.hsSections): nodeName = "Section"; break;
default:
return null;
}
var node = doc.Descendants(ns + nodeName).Where(n => n.Attribute("name").Value == objectName).FirstOrDefault();
return node.Attribute("ID").Value;
}
static string GetPageContent(string pageId)
{
string xml;
onenoteApp.GetPageContent(pageId, out xml, OneNote.PageInfo.piAll);
var doc = XDocument.Parse(xml);
var outLine = doc.Descendants(ns + "Outline").First();
var content = outLine.Descendants(ns + "T").First();
string contentVal = content.Value;
content.Value = "modified";
onenoteApp.UpdatePageContent(doc.ToString());
return null;
}
This is just what I've gleaned from reading examples on the web (of course, you've already read all of those) and peeking into the way OneNote stores its data in XML using ONOMspy (http://blogs.msdn.com/b/johnguin/archive/2011/07/28/onenote-spy-omspy-for-onenote-2010.aspx).
If you want to work with OneNote content, you'll need a basic understanding of XML. Writing text to a OneNote page involves creating an outline element, whose content will be contained in OEChildren elements. Within an OEChildren element, you can have many other child elements representing outline content. These can be of type OE or HTMLBlock, if I'm reading the schema correctly. Personally, I've only ever used OE, and in this case, you'll have an OE element containing a T (text) element. The following code will create an outline XElement and add text to it:
// Get info from OneNote
string xml;
onApp.GetHierarchy(null, OneNote.HierarchyScope.hsSections, out xml);
XDocument doc = XDocument.Parse(xml);
XNamespace ns = doc.Root.Name.Namespace;
// Assuming you have a notebook called "Test"
XElement notebook = doc.Root.Elements(ns + "Notebook").Where(x => x.Attribute("name").Value == "Test").FirstOrDefault();
if (notebook == null)
{
Console.WriteLine("Did not find notebook titled 'Test'. Aborting.");
return;
}
// If there is a section, just use the first one we encounter
XElement section;
if (notebook.Elements(ns + "Section").Any())
{
section = notebook.Elements(ns + "Section").FirstOrDefault();
}
else
{
Console.WriteLine("No sections found. Aborting");
return;
}
// Create a page
string newPageID;
onApp.CreateNewPage(section.Attribute("ID").Value, out newPageID);
// Create the page element using the ID of the new page OneNote just created
XElement newPage = new XElement(ns + "Page");
newPage.SetAttributeValue("ID", newPageID);
// Add a title just for grins
newPage.Add(new XElement(ns + "Title",
new XElement(ns + "OE",
new XElement(ns + "T",
new XCData("Test Page")))));
// Add an outline and text content
newPage.Add(new XElement(ns + "Outline",
new XElement(ns + "OEChildren",
new XElement(ns + "OE",
new XElement(ns + "T",
new XCData("Here is some new sample text."))))));
// Now update the page content
onApp.UpdatePageContent(newPage.ToString());
Here's what the actual XML you're sending to OneNote looks like:
<Page ID="{20A13151-AD1C-4944-A3D3-772025BB8084}{1}{A1954187212743991351891701718491104445838501}" xmlns="http://schemas.microsoft.com/office/onenote/2013/onenote">
<Title>
<OE>
<T><![CDATA[Test Page]]></T>
</OE>
</Title>
<Outline>
<OEChildren>
<OE>
<T><![CDATA[Here is some new sample text.]]></T>
</OE>
</OEChildren>
</Outline>
</Page>
Hope that helps get you started!
If you're using C#, Check out the newer OneNote REST API at http://dev.onenote.com. It already supports creating a new page and has a beta API to patch and add content to an existing page.
What I want to do is that I have created a asp.net web application and right now I m using XML to save my data (I AM NOT USING SQL)and my next task is to Generate A unique 8digits Alphanumeric Id for the users which will be used by them In future to track there request.
I have been given a particular format of this ID so I can't use 'GUID'.
The format given is this:
PR000000(PR stands for Project)
NP000000(NP stands for Non-Project)
So, the first two letters will be selected according to the user entry(if its a Project procurement request then 'PR' and in case of Non-project Procurement request 'NP') and rest 6 digits are simple integers which will keep increasing (000001,000002,000003....999999).
I have searched a lot everywhere but its all about php,uniquecode,GUID etc. but I just want a simple code which perform the above task with ease like IF-Else statement + keep increasing the count(000001...000002..).
This is the code for Project and Non project selection:
PROJECT<input type="radio" name="portal" id="radio1" onclick="changeMe(this);"/> <input type="text" name="textprojectOff" id="text1" value="Project Name" onclick="changeMe(this);"/>
NON-PROJECT<input type="radio" id="radio2" name="portal" onclick="changeMe(this)"/> <input type="text" name="textnonprojectOff" id="text2" value="Department" onclick="changeMe(this);"/>
<script type="text/javascript" >
function changeMe(inField)
{
var fieldId = inField.id;
var type = fieldId.substring(0, 4);
if (type == 'text') {
var name = fieldId.substring(4);
var radioButton = document.getElementById("radio" + name);
radioButton.checked = true;
} else {
var name = fieldId.substring(5);
var textField = document.getElementById("text" + name);
textField.focus();
textField.value ='';
}
}
If anyone require my xml code for my help then kindly tell me in the comment section I will post it at that time.
For your requirement, you will have to generate the unique ids from server-side code after checking the existing values in a Database.
you can use a Oracle Sequence if you want. Client side generation should not be used in this case because there is a change that multiple users can get the same unique id. So this generation should be handled server side / db side.
Here I got the solution of My Own problem ,So,I thought to share it with everyone so that other can get help from it. Without Using any typical Function I have generated an Unique Id for each and ever user.
.cs programming:
protected void GetId()
{
MSXML2.DOMDocument objXML = new MSXML2.DOMDocument();
string oPath = null;
oPath = Server.MapPath("PARNId.xml");
XmlDocument doc = new XmlDocument();
if (objXML.load(oPath) == true)
{
objNextId = objXML.selectSingleNode("//Id").text;
}
}
protected void SetId()
{
MSXML2.DOMDocument objXML = new MSXML2.DOMDocument();
string oPath = null;
oPath = Server.MapPath("PARNId.xml");
XmlDocument doc = new XmlDocument();
if (objXML.load(oPath) == true)
{
objXML.selectSingleNode("//Id").text = objNextId;
}
if (RadioButton2.Checked == true)
{
Label1.Text = "Your PURCHASE ACTION REQUEST NUMBER Is : PR" + objNextId;
}
else
{
Label1.Text = "Your PURCHASE ACTION REQUEST NUMBER Is :NP" + objNextId;
}
objXML.save(oPath);
}
Calling both the functions
GetId();
Label1.Text = objNextId;
objNextId = Convert.ToString(Convert.ToInt32(objNextId) + 1);
SetId()
I have created a separate XML file named Track.xml, in which I have assigned a node 100000,and this keep incrementing with every request made and finally I saved the label1.text and this keep record of all PARN generated with each user Hope it helps other!!
I'm building an application which should watch file for access, reading, writing, deleting.
I'm using the built in auditing system on a Windows 7 Pro. You turn it on in gpedit.msc, and then set the audit flags for the files you want to watch, and then you get entries in the security log.
What I want to do is watching the security log in real time, which I do like this:
static EventLog securityLog = new EventLog("Security", System.Environment.MachineName);
securityLog.EntryWritten += new EntryWrittenEventHandler(OnEntryWritten);
securityLog.EnableRaisingEvents = true;
This works and calls my OnEntryWritten-Function.
public static void OnEntryWritten(object source, EntryWrittenEventArgs entry)
entry.Entry is the EntryWrittenEventArgs.Entry Property, which doesn't seem to give me any access to the XML-properties of the entry, which I need, beecause it contains additional information.
What I'm trying to do afterwards is to query the event log via another EventLogReader, because I can get entry.Entry.Index which should be the eventInstance.RecordId of an event that I get from the EventLogReader.
<QueryList>
<Query Id="0" Path="Security">
<Select Path="Security">">*[System[(EventRecordID=181616)]]</Select>
</Query>
</QueryList>
works as XPath query directly in the event log, it gives back just one entry.
string query = "*[System[(EventRecordID=" + entry.Entry.Index + ")]]";
// Create Event Log Query and Reader
EventLogQuery eventsQuery = new EventLogQuery("Security",
PathType.LogName,
query);
EventLogReader logReader = new EventLogReader(eventsQuery);
// For each event returned from the query
for (EventRecord eventInstance = logReader.ReadEvent(); eventInstance != null; eventInstance = logReader.ReadEvent())
{
if (eventInstance.RecordId == entry.Entry.Index) //RecordId and Index are the same thing: the identifier of the record/entry.
{
XDocument xml;
try
{
xml = XDocument.Parse(logReader.ReadEvent().ToXml());
}
catch (Exception e)
{
//logger.Write(e.Message.ToString());
break; //We seem to have a newline character in the logReader.ReadEvent() sometimes, but nothing else, so we can safely break here or completely ignore it.
}
This fails when I try to get the xml, why is that?
I get an "Object reference not set to an instance of an object." which is an System.NullReferenceException. I'm not sure how this error actually can happen.
If I query the log like this
EventLogQuery eventsQuery = new EventLogQuery("Security",
PathType.LogName,
"*[EventData[Data[#Name='ObjectType'] and (Data='File')]] ");
it works without a problem.
What's the best way to do this, anyway?
The InstanceId does not return the same value as the index value.
Try the following snippet to get the correct ID
UInt16 eventid = (UInt16)(entry.Entry.InstanceId)
Have you checked the output of XDocument.Parse(logReader.ReadEvent().ToXml()) ? Does ToXml() generate a proper XDocument (with header, root element...)? That might be the problem.
However if you don't stick to this solution, you could try to use FileSystemWatcher or the official file monitoring tool from Microsoft: Filemon
Every time you call ReadEvent() you are retrieving the next event. Your query only returns one event. The line
xml = XDocument.Parse(logReader.ReadEvent().ToXml());
is the culprit.
Your code should look more like this:
string query = "*[System[(EventRecordID=" + entry.Entry.Index + ")]]";
// Create Event Log Query and Reader
EventLogQuery eventsQuery = new EventLogQuery("Security",
PathType.LogName,
query);
EventLogReader logReader = new EventLogReader(eventsQuery);
EventRecord eventInstance = logReader.ReadEvent();
if (eventInstance != null)
{
XDocument xml;
try
{
xml = XDocument.Parse(eventInstance.ToXml());
}
catch (Exception e)
{
//This probably won't happen now.
break; //We seem to have a newline character in the
}
}